| 1. |
(define (fact n) |
|
| 2. | (define x 4) | GE |
|
| 3. | (define make-counter |
|
| The key thing to notice in this example is that the days and dollars procedures have different instances of the variable count, because their double-bubbles (L3 and L5) point to different frames. When days increments its count, nothing happens to dollar's count. | ||
| 4. | (define make-count-proc-1 |
|
| We see that this definition doesn't work -- the let expression that creates the count variable is so deep within the function that it's actually recreated on every call to sqrt*. So when we call (sqrt* 'count) expecting to get the value of the counter, all we ever get is 0. | ||
| 5. | (define make-count-proc-2 |
|
| Now the let expression is placed in such a way that every procedure created by make-count-proc-2 has a different instance of count. Thus, sqrt* counts separately from square*, which is probably what we want. | ||
| 6. | (define make-count-proc-3 |
|
| Now we see that making let the outermost expression makes
only one instance of the count variable that is shared by all
procedures produced by make-count-proc-3. The calls
to sqrt* and square* both increment the same
count variable, and we'll get
the same value regardless of whether we ask sqrt* or square* for the
count. | ||