SICP 全笔记

Exercise 3.14. The following procedure is quite useful, although obscure:

(define (mystery x)
  (define (loop x y)
    (if (null? x)
        y
        (let ((temp (cdr x)))
          (set-cdr! x y)
          (loop temp x))))
  (loop x '()))

Loop uses the ‘‘temporary’’ variable temp to hold the old value of the cdr of x, since the set-cdr! on the next line destroys the cdr. Explain what mystery does in general. Suppose v is defined by (define v (list ‘a ‘b ‘c ‘d)). Draw the box-and-pointer diagram that represents the list to which v is bound. Suppose that we now evaluate (define w (mystery v)). Draw box-and-pointer diagrams that show the structures v and w after evaluating this expression. What would be printed as the values of v and w ?

因为 loop 使用迭代的方式完成的这个操作,我们可以把参数写出来:

(loop '(a b c d) '())
(loop '(b c d) '(a))
(loop '(c d) '(b a))
(loop '(d) '(c b a))
(loop '() '(d c b a))

可以看出,最后得到了逆序。因为 loop 中使用 set-cdr! 来改变链表,这使得传递进来的参数也发生了改变。

刚开始时,v 是

v       +---+---+      +---+---+      +---+---+      +---+---+
  ----->|   |   +----->|   |   +----->|   |   +----->|   | / |
        +-+-+---+      +-+-+---+      +-+-+---+      +-+-+---+
          |              |              |              |
          |              |              |              |
          |              |              |              |
          v              v              v              v
        +---+          +---+          +---+          +---+
        | a |          | b |          | c |          | d |
        +---+          +---+          +---+          +---+

经过 mystery 过程之后,变为

w                                               v
      +---+---+      +---+---+      +---+---+      +---+---+
----->|   |   +----->|   |   +----->|   |   +----->|   | / |
      +-+-+---+      +-+-+---+      +-+-+---+      +-+-+---+
        |              |              |              |
        |              |              |              |
        |              |              |              |
        v              v              v              v
      +---+          +---+          +---+          +---+
      | d |          | c |          | b |          | a |
      +---+          +---+          +---+          +---+

结果即为:w(d c b a)v(a)