Skip to content

Instantly share code, notes, and snippets.

@thisiswei
Created February 19, 2013 07:11
Show Gist options
  • Save thisiswei/4983716 to your computer and use it in GitHub Desktop.
Save thisiswei/4983716 to your computer and use it in GitHub Desktop.
(define fib3
(letrec([memo null] ; list of pairs (arg . result)
[f (lambda (x)
(let ([ans (assoc x memo)])
(if ans
(cdr ans)
(let ([new-ans (if (or (= x 1) (= x 2))
1
(+ (f (- x 1))
(f (- x 2))))])
(begin
(set! memo (cons (cons x new-ans) memo))
new-ans)))))])
f))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment