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: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
type SchemeType = { Value: decimal; Rate: decimal }

module ITax =
    let relu = max 0m

    let calc scheme x =
        let rec calcRecursive scheme agg =
            match scheme with
            | [ y ] -> agg + (relu ((x - y.Value) * y.Rate))
            | lb :: ub :: rest ->
                if x <= ub.Value then
                    agg + (relu ((x - lb.Value) * lb.Rate))
                else
                    calcRecursive (ub :: rest) (agg + (relu ((ub.Value - lb.Value) * lb.Rate)))
            | _ -> agg

        (calcRecursive (scheme |> List.sortBy (fun x -> x.Value)) 0m)
        / 100m

let tryDecimal x =
    try
        x |> decimal |> Some
    with _ -> None

[<EntryPoint>]
let main argv =
    let scheme =
        [ { Value = 2_50_000m; Rate = 5m }
          { Value = 5_00_000m; Rate = 10m }
          { Value = 7_50_000m; Rate = 15m }
          { Value = 10_00_000m; Rate = 20m }
          { Value = 12_50_000m; Rate = 25m }
          { Value = 15_00_000m; Rate = 30m } ]

    let incomeTaxCalculator = ITax.calc scheme

    argv
    |> Seq.choose tryDecimal
    |> Seq.iter (fun x -> printfn "Income tax for ammount- Rs. %.3f = Rs. %.3f Only/-" x (x |> incomeTaxCalculator))

    0
SchemeType.Value: decimal
Multiple items
val decimal : value:'T -> decimal (requires member op_Explicit)

--------------------
type decimal = System.Decimal

--------------------
type decimal<'Measure> = decimal
SchemeType.Rate: decimal
val relu : (decimal -> decimal)
val max : e1:'T -> e2:'T -> 'T (requires comparison)
val calc : scheme:SchemeType list -> x:decimal -> decimal
val scheme : SchemeType list
val x : decimal
val calcRecursive : (SchemeType list -> decimal -> decimal)
val agg : decimal
val y : SchemeType
val lb : SchemeType
val ub : SchemeType
val rest : SchemeType list
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
    interface IReadOnlyList<'T>
    interface IReadOnlyCollection<'T>
    interface IEnumerable
    interface IEnumerable<'T>
    member GetReverseIndex : rank:int * offset:int -> int
    member GetSlice : startIndex:int option * endIndex:int option -> 'T list
    member Head : 'T
    member IsEmpty : bool
    member Item : index:int -> 'T with get
    member Length : int
    ...
val sortBy : projection:('T -> 'Key) -> list:'T list -> 'T list (requires comparison)
val x : SchemeType
val tryDecimal : x:string -> decimal option
val x : string
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
Multiple items
type EntryPointAttribute =
  inherit Attribute
  new : unit -> EntryPointAttribute

--------------------
new : unit -> EntryPointAttribute
val main : argv:string [] -> int
val argv : string []
val incomeTaxCalculator : (decimal -> decimal)
module ITax

from Script
module Seq

from Microsoft.FSharp.Collections
val choose : chooser:('T -> 'U option) -> source:seq<'T> -> seq<'U>
val iter : action:('T -> unit) -> source:seq<'T> -> unit
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

More information

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