When we introduced the substitution model in
section we showed how the combination
`(f 5)` evaluates to 136, given the following procedure
definitions:

(define (square x) (* x x)) (define (sum-of-squares x y) (+ (square x) (square y))) (define (f a) (sum-of-squares (+ a 1) (* a 2)))We can analyze the same example using the environment model. Figure shows the three procedure objects created by evaluating the definitions of

In figure we see the environment structure created
by evaluating the expression `(f 5)`. The call to `f` creates
a new environment E1 beginning with a frame in which `a`, the
formal parameter of `f`, is bound to the argument 5. In E1, we
evaluate the body of `f`:

(sum-of-squares (+ a 1) (* a 2))

To evaluate this combination, we first evaluate the subexpressions.
The first subexpression, `sum-of-squares`, has a value that is a
procedure object. (Notice how this value is found: We first look in
the first frame of E1, which contains no binding for `
sum-of-squares`. Then we proceed to the enclosing environment,
i.e. the global environment, and find the binding shown in
figure .) The other two subexpressions are
evaluated by applying the primitive operations `+` and `*` to
evaluate the two combinations `(+ a 1)` and `(* a 2)` to
obtain 6 and 10, respectively.

Now we apply the procedure object `sum-of-squares` to the
arguments 6 and 10. This results in a new environment E2 in which the
formal parameters `x` and `y` are bound to the arguments.
Within E2 we evaluate the combination `(+ (square x) (square y))`.
This leads us to evaluate `(square x)`, where `square` is
found in the global frame and `x` is 6. Once again, we set up a
new environment, E3, in which `x` is bound to 6, and within this
we evaluate the body of `square`, which is `(* x x)`. Also as
part of applying `sum-of-squares`, we must evaluate the
subexpression `(square y)`, where `y` is 10. This second call
to `square` creates another environment, E4, in which `x`, the
formal parameter of `square`, is bound to 10. And within E4 we
must evaluate `(* x x)`.

The important point to observe is that each call to `square`
creates a new environment containing a binding for `x`. We can
see here how the different frames serve to keep separate the different
local variables all named `x`. Notice that each frame created by
`square` points to the global environment, since this is the
environment indicated by the `square` procedure object.

After the subexpressions are evaluated, the results are
returned. The values generated by the two calls to `square` are
added by `sum-ofsquares`, and this result is returned by `f`.
Since our focus here is on the environment structures, we will not
dwell on how these returned values are passed from call to call;
however, this is also an important aspect of the evaluation process,
and we will return to it in detail in chapter 5.

**Exercise.**
In section
we used the substitution model to analyze two
procedures for computing factorials, a recursive version

(define (factorial n) (if (= n 1) 1 (* n (factorial (- n 1)))))and an iterative version

(define (factorial n) (fact-iter 1 1 n)) (define (fact-iter product counter max-count) (if (> counter max-count) product (fact-iter (* counter product) (+ counter 1) max-count)))Show the environment structures created by evaluating