Created
January 6, 2016 05:28
-
-
Save pocarist/85c7f578a690ee882360 to your computer and use it in GitHub Desktop.
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
open System | |
let memo_rec f = | |
let m = ref Map.empty in | |
let rec g x = | |
match Map.tryFind x !m with | |
| Some ans -> ans | |
| None -> | |
let y = f g x in | |
m := Map.add x y !m | |
y | |
in | |
g | |
let fib = | |
let rec f self = function | |
| 0 -> 0L | |
| 1 -> 1L | |
| 2 -> 1L | |
| i -> | |
let p1 = self (i-1) in | |
let p2 = self (i-2) in | |
p1 + p2 | |
in | |
memo_rec f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment