SICP:3.3.1 Mutable List Structure
SICP:3.3.1 Mutable List Structure
本文逐段翻译 SICP 第二版 §3.3.1《Mutable List Structure》,覆盖
set-car!、set-cdr!、append!、cycles、sharing、identity、procedural pairs 与 Exercises 3.12–3.20;保留 Figure 3.12–3.17、复杂度公式和 Scheme code。
TL;DR
cons、car、cdr能构造和读取 list structure,但不能修改已有 pairs;set-car!与set-cdr!提供 mutation。append创建新结构,append!splice 现有结构;二者对 aliases 和后续观察的影响不同。- Sharing 让多个 data objects 指向同一 pair;mutation 后,原本相同的 lists 可能产生不同 behavior。
- Cycles、重复计数和 identity 都要求程序显式考虑 pointer sharing,而不能只把 list 当作 value 的树。
名词解释
| 名词 | 含义 | 本节对应物 |
|---|---|---|
| mutator | 修改 existing data structure 的 operation | set-car!、set-cdr! |
| sharing | 多个 references 指向同一 pair | (cons x x) |
| identity | object 作为同一个可变实体的持续性 | z1 中共享的 x |
| cycle | cdr pointers 最终回到已访问 pair | make-cycle |
| procedural pair | 用 dispatch procedure 表示的 pair | cons / car / cdr |
目录
| 章节 | 说明 |
|---|---|
| 3.3.1 Mutable List Structure | mutable pairs |
| Sharing and identity | aliasing 与 object identity |
| Mutation is just assignment | procedural mutable data |
| Exercises 3.12–3.20 | list mutation exercises |
3.3.1 Mutable List Structure
Pairs 的 basic operations——cons、car 和 cdr——可以构造 list structure,也可以 select list structure 的 parts,但不能修改 list structure。append 和 list 等 list operations 也不能修改 structure,因为它们都能用 cons、car 和 cdr 定义。要修改 list structures,需要新的 operations。

Figure 3.12: Lists x: ((a b) c d) 与 y: (e f)。

Figure 3.13: 对 Figure 3.12 中 lists 执行 (set-car! x y) 的 effect。

Figure 3.14: 执行 (define z (cons y (cdr x))) 的 effect。

Figure 3.15: 对 Figure 3.12 中 lists 执行 (set-cdr! x y) 的 effect。
Pairs 的 primitive mutators 是 set-car! 和 set-cdr!。set-car! 接收两个 arguments,第一个必须是 pair;它修改该 pair,把 car pointer 替换为指向 second argument 的 pointer。1
假设 x 绑定到 ((a b) c d),y 绑定到 (e f)。Evaluate (set-car! x y) 会修改 x 所绑定的 pair,把它的 car 替换为 y 的 value;x 现在打印为 ((e f) c d),表示 (a b) 的 pairs 从 original structure detach。2
Evaluate (define z (cons y (cdr x))) 时,z 绑定到 cons 创建的 new pair,而 x 所绑定的 list unchanged。set-cdr! 与 set-car! 类似,但替换 cdr pointer;对 Figure 3.12 执行 (set-cdr! x y) 会 detach 原来作为 x 的 cdr 的 (c d)。
cons 创建 new pairs;set-car! 和 set-cdr! 修改 existing pairs。理论上可以用 mutators 和 get-new-pair 实现 cons:
(define (cons x y)
(let ((new (get-new-pair)))
(set-car! new x)
(set-cdr! new y)
new))
Exercises 3.12–3.20
Exercise 3.12. append 通过 successively consing x elements onto y 创建 new list;append! 则 splice lists,修改 x 的 final pair,使 cdr 指向 y:
(define (append x y)
(if (null? x)
y
(cons (car x) (append (cdr x) y))))
(define (append! x y)
(set-cdr! (last-pair x) y)
x)
(define (last-pair x)
(if (null? (cdr x))
x
(last-pair (cdr x))))
对于 (define x (list 'a 'b))、(define y (list 'c 'd)),分别计算 (cdr x) 在 (append x y) 和 (append! x y) 之后的 responses,并画 box-and-pointer diagrams。
Exercise 3.13. make-cycle:
(define (make-cycle x)
(set-cdr! (last-pair x) x)
x)
画出 (define z (make-cycle (list 'a 'b 'c))) 的 structure,并说明 (last-pair z) 的 behavior。
Exercise 3.14. 解释 mystery 一般做什么,并画出 v、w 在 (define w (mystery v)) 后的 structures:
(define (mystery x)
(define (loop x y)
(if (null? x)
y
(let ((temp (cdr x)))
(set-cdr! x y)
(loop temp x))))
(loop x '()))
temp 保存 old cdr,因为下一行 set-cdr! 会 destroy 它。
Sharing and identity
考虑:
(define x (list 'a 'b))
(define z1 (cons x x))
如 Figure 3.16,z1 的 car 和 cdr 都指向同一个 pair x。使用 cons 构造 lists,通常会产生 interlinked pair structure,许多 pairs 可能被不同 structures shared。

Figure 3.16: (cons x x) 形成的 z1。
对比:
(define z2 (cons (list 'a 'b) (list 'a 'b)))
Figure 3.17 中两个 (a b) lists 的 pairs distinct,虽然 symbols shared。3

Figure 3.17: (cons (list 'a 'b) (list 'a 'b)) 形成的 z2。
作为 list,z1 和 z2 都表示 ((a b) a b);只使用 cons、car、cdr 时 sharing undetectable,但 mutators 使 sharing significant:
(define (set-to-wow! x)
(set-car! (car x) 'wow)
x)
z1 中 car 与 cdr 是 same pair,修改 car 也修改 cdr;z2 中两者 distinct,只修改 car:
z1
((a b) a b)
(set-to-wow! z1)
((wow b) wow b)
z2
((a b) a b)
(set-to-wow! z2)
((wow b) a b)
eq? 可用于 detect sharing:(eq? x y) 判断 x 和 y 是否是 same object。因此 (eq? (car z1) (cdr z1)) 为 true,而 (eq? (car z2) (cdr z2)) 为 false。
Sharing 能扩展 pairs 可表示的数据 structures,但 mutation 也可能影响其他 shared structures;除非理解 data objects 如何 sharing,否则 set-car! 和 set-cdr! 会产生 unanticipated results。4
Exercises 3.15–3.19
Exercise 3.15. 画 box-and-pointer diagrams,解释 set-to-wow! 对 z1、z2 的 effect。
Exercise 3.16. 证明下面的 count-pairs 不正确,并画出恰好由 three pairs 组成、使它返回 3、4、7 或永远不 return 的 structures:
(define (count-pairs x)
(if (not (pair? x))
0
(+ (count-pairs (car x))
(count-pairs (cdr x))
1)))
Exercise 3.17. 设计 correct count-pairs,返回任意 structure 中 distinct pairs 数量;维护一个记录 already-counted pairs 的 auxiliary data structure。
Exercise 3.18. 写 procedure 判断 list 是否 contains cycle。
Exercise 3.19. 用 constant-space algorithm 重做 Exercise 3.18。
Mutation is just assignment
Pairs 可以用 procedures 表示:
(define (cons x y)
(define (dispatch m)
(cond ((eq? m 'car) x)
((eq? m 'cdr) y)
(else (error "Undefined operation -- CONS" m))))
dispatch)
(define (car z) (z 'car))
(define (cdr z) (z 'cdr))
同样,可以用 assignment 和 local state 表示 mutable data:
(define (cons x y)
(define (set-x! v) (set! x v))
(define (set-y! v) (set! y v))
(define (dispatch m)
(cond ((eq? m 'car) x)
((eq? m 'cdr) y)
((eq? m 'set-car!) set-x!)
((eq? m 'set-cdr!) set-y!)
(else (error "Undefined operation -- CONS" m))))
dispatch)
(define (car z) (z 'car))
(define (cdr z) (z 'cdr))
set-car! 与 set-cdr! 通过 dispatch procedure 取得 mutator,再对 new value apply。理论上,assignment 就足以 account for mutable data 的 behavior;一旦 language 接受 set!,mutable data 的 all issues 也随之出现。5
Exercise 3.20
使用 procedural pairs implementation,画 environment diagrams,说明:
(define x (cons 1 2))
(define z (cons x x))
(set-car! (cdr z) 17)
(car x)
17
并与 Exercise 3.11 比较。
图与格式复核
- 保留 Figure 3.12–3.17 的 6 个原始 GIF,以及复杂度公式中的原始 GIF。
- 显示宽度按原始 GIF pixel width 保留:279、279、279、279、174、262、9。
- 保留
append!、make-cycle、mystery、sharing、cycle detection 与 procedural mutable pair 的 Scheme code。
术语对照
| English | 中文处理 |
|---|---|
| mutable list structure | mutable list structure |
| mutator | mutator |
| sharing / aliasing | sharing / aliasing |
| identity | identity |
| cycle | cycle |
| box-and-pointer diagram | box-and-pointer diagram |
参考资料
-
set-car!的 first argument 必须是 pair;它改变 pair 的carpointer。 ↩ -
被替换的 pairs 如果没有其他 pointers,之后可能成为 garbage;本节关注 structure semantics。 ↩
-
Symbols 可以共享;这里 distinct 指的是 list pairs distinct。 ↩
-
Mutation 与 aliasing 的核心风险是:局部 modification 可能通过 shared pointer 影响远端 object。 ↩
-
Mutable data 的 procedural implementation 与 message passing 直接相关;state 由 pair fields 或 procedure closure 保存。 ↩
评论 (0)