sicp-ex-5.26



<< Previous exercise (5.25) | Index | Next exercise (5.27) >>


meteorgan

  
  
  
 (a) 
 n     total-pushes    maximum-depth 
 1        64                        10 
 2        99                        10 
 3      134                        10 
 so the maximum-depth is 10 
 (b) 
 total-pushes = 35*n + 29 

codybartfast




By the way, the maximum depth is not constant with Normal application:

┌───────────────────────┬──────────────────┬──────────────────┐
│                       │   Maximum Depth  │ Number of Pushes │
├───────────────────────┼──────────────────┼──────────────────┤
│ Factorial Applicative │      0n + 10     │     35n + 35     │
├───────────────────────┼──────────────────┼──────────────────┤
│    Factorial Normal   │      6n +  3     │     50n + 48     │
└───────────────────────┴──────────────────┴──────────────────┘

The stack does not grow during the construction of the answer, instead the
answer contains nested thunks.  So I think forcing the answer to an actual
value causes recursive calls to actual-value, eval-dispatch, and force-it,
which grows the stack.  Of course a better design than mine might eliminate
this, especially if we didn't have memoization.