Skip to content

Instantly share code, notes, and snippets.

@pocarist
Created January 6, 2016 05:28
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 pocarist/85c7f578a690ee882360 to your computer and use it in GitHub Desktop.
Save pocarist/85c7f578a690ee882360 to your computer and use it in GitHub Desktop.
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