sicp-ex-3.10



<< Previous exercise (3.9) | Index | Next exercise (3.11) >>


Does this make sense?

(define W1 (make-withdraw 100))

When make-withdraw is evaluated, E0 is created with Frame A having the
initial-mount binding. Next, as a result of the evaluation of the 
anonymous function (generated by the set (Edit: let?) structure), Frame B is created
with the binding of balance (E1 is the pointer to this frame).

         _______________________
global->| make-withdraw : *     |
env.    | W1 :  *         |     |
         -------|---^-----|---^-
                |   |     |   |
                |   |     parameter: initial-mount
                |   |     body: ((lambda (balance) ((...))) initial-mount)
                |   |
                |  _|___Frame_A__________
                | | initial-mount : 100  |<- E0
                |  -^--------------------
                |   |
                |  _|__________Frame_B______
                | | balance : initial-mount | <- E1
                |  -^-----------------------
                |   |
                parameter: amount
                body: (if (>= balance amount) ... )

(W1 50)

Set! will affect Frame B, initial-mount remains unchanged in Frame A. 
         _______________________
global->| make-withdraw : *     |
env.    | W1 :  *         |     |
         -------|---^-----|---^-
                |   |     |   |
                |   |     parameter: initial-mount
                |   |     body: ((lambda (balance) ((...))) initial-mount)
                |   |
                |  _|___Frame_A__________
                | | initial-mount : 100  |<- E0
                |  -^--------------------
                |   |
                |  _|__________Frame_B___
                | | balance : 50         | <- E1
                |  -^--------------------
                |   |
                parameter: amount
                body: (if (>= balance amount) ... )

Amy

It's interesting to see that the redundant lambda expression created a redundant frame. The W1 procedure will only interact with the second created frame that has the value of balance.


LisScheSic

To be complete, we will do the following before balance : 50 (same as the addition of Figure 3.8 based on Figure 3.7):

         _______________________
global->| make-withdraw : *     |
env.    | W1 :  *         |     |
         -------|---^-----|---^-
                |   |     |   |
                |   |     ......
                |   |     
                |   |
                |  _|___Frame_A__________
                | | ............         |<- E0
                |  -^--------------------
                |   |
                |  _|__________Frame_B___
                | | balance : 100        | <- E1
                |  -^---------------^----
                |   |               |    -------------
                ......              -----|amount: 50 |
                                         -------------
                                       (if (>= balance amount) ...)

W2 will do the same addition as what Figure 3.10 adds based on Figure 3.9 by creating one new instance same as W1 (Here we will create Frame_C and Frame_D) but sharing the code part.