Simple Memoize In Scheme
Here is some simple code to memoize a recursive function in Scheme. I originally got this from a Stackoverflow question, but the link is now dead, so I thought I will post it up here.
Memoize function:
(define (memoize op) (letrec ((get (lambda (key) (list #f))) (set (lambda (key item) (let ((old-get get)) (set! get (lambda (new-key) (if (equal? key new-key) (cons #t item) (old-get new-key)))))))) (lambda args (let ((ans (get args))) (if (car ans) (cdr ans) (let ((new-ans (apply op args))) (set args new-ans) new-ans)))))) After we have memoized this, we can use this within our recursive function.
[Read More]