Be careful -- this doesn't fully memoize recursive functions unless you force the computation of intermediate results, since the recursive calls don't use memoization:
(define memo-fib (memoize fib)) ;; example from SICP
(memo-fib 40)
;; long wait
I would love to see a good general-purpose 'memoize but rather doubt it's possible, though I think I remember seeing one in Common Lisp in "Paradigms of Artificial Intelligence Programming" that exploited CL's weird namespacing rules for functions to make recursive functions like 'fib run fast.
You can solve the problem with the parent's memoization by writing the original fib using open recursion and closing it with a fixed-point operator. Doesn't help for library functions or things like that, though.
(define memo-fib (memoize fib)) ;; example from SICP (memo-fib 40) ;; long wait
I would love to see a good general-purpose 'memoize but rather doubt it's possible, though I think I remember seeing one in Common Lisp in "Paradigms of Artificial Intelligence Programming" that exploited CL's weird namespacing rules for functions to make recursive functions like 'fib run fast.