SICP 全笔记

Exercise 3.13. Consider the following make-cycle procedure, which uses the last-pair procedure defined in exercise 3.12:

(define (make-cycle x)
  (set-cdr! (last-pair x) x)
  x)

Draw a box-and-pointer diagram that shows the structure z created by

(define z (make-cycle (list 'a 'b 'c)))

What happens if we try to compute (last-pair z)?

make-cycle 会把链表最后一个盒子的 cdr 指针指向第一个盒子,于是 z 就变成了

z    +-------------------------------+
|    |                               |
|    |                               |
|    v                               |
|  +---+---+     +---+---+     +---+-+-+
+->|   |   +---->|   |   +---->|   |   |
   +-+-+---+     +-+-+---+     +-+-+---+
     |             |             |
     |             |             |
     v             v             v
   +---+         +---+         +---+
   | a |         | b |         | c |
   +---+         +---+         +---+

试图去计算 z 的最后一个 pair 会进入死循环。