(define (make-table) (cons '() '())) (define (lookup wat table) (let ((answer (assoc wat (car table)))) (if answer (cdr answer) #f))) (define (insert! wat1 wat2 table) (set-car! table (cons (cons wat1 wat2) (car table)))) ;(define (memoize f) ; (let ((table (make-table))) ; (define (lookup-or-compute x) ; (let ((previously-computed-result (lookup x table))) ; (if (not (null? previously-computed-result)) ; previously-computed-result ; (let ((result (f x))) ; (insert! x result table) ; result)))) ; lookup-or-compute)) (define (memoize f) (let ((table (make-table))) (define (lookup-or-compute x) (let ((previously-computed-result (lookup x table))) (if (not (null? previously-computed-result)) (begin (display "Memo-fib ") (display x) (display " uit tabel") (newline) previously-computed-result) (begin (display "Memo-fib ") (display x) (display " berekent") (newline) (let ((result (f x))) (insert! x result table) result))))) lookup-or-compute)) (define memo-fib (memoize (lambda (n) (cond ((= n 0) 0) ((= n 1) 1) (else (+ (memo-fib (- n 1)) (memo-fib (- n 2)))))))) (define (fib n) (cond ((= n 0) 0) ((= n 1) 1) (else (+ (fib (- n 1)) (fib (- n 2)))))) Memo-fib 3 berekent Memo-fib 2 berekent Memo-fib 1 berekent Memo-fib 0 berekent Memo-fib 1 uit tabel