2 people like it.

Progressive tax calculator

Calculates progressive tax on an annual income.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
/// Calculates progressive tax on annual income
let calcProgressiveTax taxBrackets income =
  taxBrackets @ [(System.Double.MaxValue, 0.0);]
  |> Seq.windowed 2
  |> Seq.fold (fun (incomeLeft, tax) bracket ->
         let bracketLo, rate = bracket.[0]
         let bracketHi = fst bracket.[1]
         let bracketWd = bracketHi - bracketLo
         max 0.0 (incomeLeft-bracketWd), tax + rate * min incomeLeft bracketWd)
       (income, 0.0)
  |> snd

// Usage
let incomeTaxBelgium2016 =  //   Taxable Income    Tax Rate on
  [                         //   Bracket, EUR      Income in Bracket
  (    0.00, 0.25);         //      0 .. 10,860      25%
  (10860.00, 0.30);         // 10,860 .. 12,470      30%
  (12470.00, 0.40);         // 12,470 .. 20,780      40%
  (20780.00, 0.45);         // 20,780 .. 38,080      45%
  (38080.00, 0.50);         // 38,080 ..  over       50%
  ]

calcProgressiveTax incomeTaxBelgium2016 70000.00
// Returns: 30,267.00
val calcProgressiveTax : taxBrackets:(float * float) list -> income:float -> float

Full name: Script.calcProgressiveTax


 Calculates progressive tax on annual income
val taxBrackets : (float * float) list
val income : float
namespace System
type Double =
  struct
    member CompareTo : value:obj -> int + 1 overload
    member Equals : obj:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    member GetTypeCode : unit -> TypeCode
    member ToString : unit -> string + 3 overloads
    static val MinValue : float
    static val MaxValue : float
    static val Epsilon : float
    static val NegativeInfinity : float
    static val PositiveInfinity : float
    ...
  end

Full name: System.Double
field float.MaxValue = 1.79769313486e+308
module Seq

from Microsoft.FSharp.Collections
val windowed : windowSize:int -> source:seq<'T> -> seq<'T []>

Full name: Microsoft.FSharp.Collections.Seq.windowed
val fold : folder:('State -> 'T -> 'State) -> state:'State -> source:seq<'T> -> 'State

Full name: Microsoft.FSharp.Collections.Seq.fold
val incomeLeft : float
val tax : float
val bracket : (float * float) []
val bracketLo : float
val rate : float
val bracketHi : float
val fst : tuple:('T1 * 'T2) -> 'T1

Full name: Microsoft.FSharp.Core.Operators.fst
val bracketWd : float
val max : e1:'T -> e2:'T -> 'T (requires comparison)

Full name: Microsoft.FSharp.Core.Operators.max
val min : e1:'T -> e2:'T -> 'T (requires comparison)

Full name: Microsoft.FSharp.Core.Operators.min
val snd : tuple:('T1 * 'T2) -> 'T2

Full name: Microsoft.FSharp.Core.Operators.snd
val incomeTaxBelgium2016 : (float * float) list

Full name: Script.incomeTaxBelgium2016

More information

Link:http://fssnip.net/7PY
Posted:3 years ago
Author:Ujjawal Sinha
Tags: finance , tax