Procedures Functions - SICP Comparison Edition" />
When we introduced the substitution model in section 1.1.5 we showed how the combination (f 5) application f(5) evaluates to 136, given the following procedure definitions: function declarations:
Original | JavaScript |
(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))) | function square(x) { return x * x; } function sum_of_squares(x, y) { return square(x) + square(y); } function f(a) { return sum_of_squares(a + 1, a * 2); } |
Original | JavaScript | |
In
figure 3.9
figure 3.10
we see the environment structure created by evaluating the expression
(f 5).
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:
Original
JavaScript
Original | JavaScript |
(sum-of-squares (+ a 1) (* a 2)) | return sum_of_squares(a + 1, a * 2); |
Now we apply the procedure function object sum-of-squares 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)). the statement return square(x) + square(y); This leads us to evaluate (square x), square(x), where square is found in the global program 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). return x * x;. Also as part of applying sum-of-squares, sum_of_squares, we must evaluate the subexpression (square y), 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). return 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 program environment, since this is the environment indicated by the square procedure function object.
After the subexpressions are evaluated, the results are returned. The values generated by the two calls to square are added by sum-of-squares, sum_of_squares, 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.
Original | JavaScript |
(define (factorial n) (if (= n 1) 1 (* n (factorial (- n 1))))) | function factorial(n) { return n === 1 ? 1 : n * factorial(n - 1); } |
Original | JavaScript |
(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))) | function factorial(n) { return fact_iter(1, 1, n); } function fact_iter(product, counter, max_count) { return counter > max_count ? product : fact_iter(counter * product, counter + 1, max_count); } |