;; A memoizing function, called "memoize". This implementation
;; assumes that a datatype TABLE has already been defined, with 
;; its associated functions MAKE-TABLE, LOOKUP and INSERT!

(define (memoize f)
  (let ((table (make-table)))
    (lambda (x)
      (let ((previously-computed-result (lookup x table)))
	(or previously-computed-result
	    (let ((result (f x)))
	      (insert! x result table)
	      result))))))

