sicp-ex-3.27



<< Previous exercise (3.26) | Index | Next exercise (3.28) >>


meteorgan



   The (memo-fib) never computes the same result twice. the call tree is a linear list. 
   If define memo-fib to be (memorize fib), the procedure won't work. because fib calls itself to compute (fib n) before applies memorize.

John-Gaines

Instead of recoding fib into memo-fib as shown in SICP (which seems like a very error-prone way of adding memoize to an existing function). The following works in DrRacket using #lang racket

 (set! fib (memoize fib)) 

It sets fib in the global environment to (memoize fib) which also causes the recursive calls within fib to call the redefined version.