Memoization
Imagine you have a long running function that you’d like to avoid running unnecessarily. For the purposes of this post you’ll have to suspend disbelief and pretend that negating a number is an expensive task. This example prints out a message so you can see when it actually gets called.
let Negate n =
printfn "Negating '%A' this is hard work" n
-n
val Negate : int -> int
> Negate 5;;
Negating '5' this is hard work
val it : int = -5
Now, let’s use that function when writing another. This function takes two numbers, negates the first, then adds the second.