SICP 3.2.3 Solutions

November 5, 2017 11:23

Exercise 3.10.

In order to assign a value to W1, a call to make-withdraw is made. This creates a new frame for that call. The let statement then creates a lambda and calls it, which means another environment is created within the one used for the call to make-withdraw. This image depicts what’s going on:

Once W1 is defined, the environment diagram will look like this:

When W1 is next called with the argument 50, an execution frame is created within the environment that W1 refers to for procedure execution (E11). Let’s call this the ‘reference environment’, to make it easier to describe. This results in the following situation on the second call:

The call results in a change to the value of balance in the reference environment of W1. After the call, the picture looks like this:

For the next step a new withdraw procedure is created, and a completely separate environment chain is created. It is similar to the first but with distinct variables. Note that the code for the procedure body is shared (this represents the actual storage in memory of the code). Any calls using W2 will be executed in its reference environment E21, and only affect the variables there.

When we compare this to the text’s version of make-withdraw, we see that the alternate version involves the creation of an additional environment. In both versions, a new frame is created when make-withdraw is called. In the second, however, the let statement causes the creation of another frame, since it executes a lambda within the environment that make-withdraw uses for evaluation. The lambda that is returned and assigned to the account is created within that second environment (E11 or E21 above). Although it never again makes use of the first frame (in which initial-value is defined), that frame still remains in the chain that is the environment.

Both procedures thus work identically, as they only modify the balance value in the course of their operation. All the let statement did was rename initial-value to balance. The procedures do not rely on anything further back in the chain of frames.

To prove that the two procedures actually do produce identical output, we can run them both through the interpreter. This is the only thing that is checked in the solutions file.

Solutions 3.2.3