Created
July 6, 2017 05:13
-
-
Save Luiz-Monad/c7c5784d731d796251293c44af429525 to your computer and use it in GitHub Desktop.
Fsharp memoize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let rec y f x = f (y f) x | |
let memoize (d:System.Collections.Generic.Dictionary<_,_>) f n = | |
if d.ContainsKey n then d.[n] | |
else d.Add(n, f n);d.[n] | |
let rec factorialucps factorial' n cont = | |
if n = 0I then cont(1I) else factorial' (n-1I) (fun k -> cont (n*k)) | |
let factorialdpcps = | |
let d = System.Collections.Generic.Dictionary<_, _>() | |
fun n -> y (factorialucps >> fun f n -> memoize d f n ) n id | |
factorialdpcps 15I //1307674368000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment