SICP 全笔记

Exercise 3.9. In section 1.2.1 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 (factorial 6) using each version of the factorial procedure.

递归版本的 environment

+-------------------------------------------------------------------------------+
|                                                                               |
|  factorial:----+                                                              |
|                |                                                              |
+----------------+--------------------------------------------------------------+
                 |       ^      ^      ^      ^      ^      ^
                 |    E1 |  E2  |   E3 |   E4 |   E5 |  E6  |
                 |     +-+-+  +-+-+  +-+-+  +-+-+  +-+-+  +-+-+
                 |     |n:6|  |n:5|  |n:4|  |n:3|  |n:2|  |n:1|
                 v     +---+  +---+  +---+  +---+  +---+  +---+
     parameter: n
          body: (if (= n 1)
                    1
                    (* n (factorial (- n 1))))

迭代版本的 environment

首先是定义后的全局环境

results of defining  factorial and fact-iter in global environment

    +---------------------------------------+
    |  fact-iter: -----------+              |
    |  factorial: ---+       |              |
    |                |       |              |
    +----------------+-------+--------------+
                     |       |
                     |       |
                     |       v
                     | Parameter: product counter max-count
                     |     body: (if (> counter max-count)..)
                     |
                     |
                     v
            Parameter: n
                 body: (fact-iter 1 1n)

在这个环境中,调用 (factorial 6)

+-----------------------------------------------------------------------------------------------------------------------+
| factorial: ...                                                                                                        |
| fact-iter: ...                                                                                                        |
+-----------------------------------------------------------------------------------------------------------------------+
     ^           ^                ^               ^               ^               ^              ^               ^
     |           |                |               |               |               |              |               |
 E1  |           |                |               |               |               |              |               |
   +-+-+   +-----+------+   +-----+------+  +-----+------+  +-----+------+  +-----+------+ +-----+------+  +-----+------+
   |n:6|   |product:1   |   |product:1   |  |product:2   |  |product:6   |  |product:24  | |product:120 |  |product:720 |
   +---+   |counter:1   |   |counter:2   |  |counter:3   |  |counter:4   |  |counter:5   | |counter:6   |  |counter:1   |
           |max-count: 6|   |max-count: 6|  |max-count: 6|  |max-count: 6|  |max-count: 6| |max-count: 6|  |max-count: 6|
           +------------+   +------------+  +------------+  +------------+  +------------+ +------------+  +------------+