Skip to content

Instantly share code, notes, and snippets.

@OdeToCode
Created December 12, 2013 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OdeToCode/7932390 to your computer and use it in GitHub Desktop.
Save OdeToCode/7932390 to your computer and use it in GitHub Desktop.
let PeriodValue payment rate period =
payment / ((1.0 + rate) ** period)
let PresentValue (factors: AnnuityFactors) =
[1 .. factors.Periods]
|> List.map (fun period -> PeriodValue factors.Payment factors.InterestRate (float period))
|> List.sum
@dahlbyk
Copy link

dahlbyk commented Dec 12, 2013

More idiomatic than the lambda would be function composition and partial application:

let PresentValue (factors: AnnuityFactors) =
    [1 .. factors.Periods]
    |> List.map (float >> PeriodValue factors.Payment factors.InterestRate)
    |> List.sum 

Also, functions are generally camelCased.

@dahlbyk
Copy link

dahlbyk commented Dec 12, 2013

You can also make this lazy, though that would only make a difference for large values of Periods:

let PresentValue (factors: AnnuityFactors) =
    seq { 1 .. factors.Periods }
    |> Seq.map (float >> PeriodValue factors.Payment factors.InterestRate)
    |> Seq.sum

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment