SICP:2.2.2 Hierarchical Structures
SICP:2.2.2 Hierarchical Structures
本文逐段翻译 SICP 第二版 §2.2.2《Hierarchical Structures》,覆盖 nested lists、trees、tree recursion、mobiles、mapping over trees 和 Exercises 2.24–2.32,保留原始图件与 Scheme code。
TL;DR
- Sequence 的 elements 自身可以是 sequences;因此 nested lists 同时也是 trees。
- Tree operations 通常通过 recursion 分解为 branches 上的 operations,直到 leaves。
map与 recursion 结合后,可以在不暴露 tree traversal details 的情况下处理整棵 tree。
目录
| 章节 | 说明 |
|---|---|
| 2.2.2 Hierarchical Structures | nested lists 与 trees |
| Exercises 2.24–2.29 | tree 与 mobile exercises |
| Mapping over trees | tree 上的 map |
| Exercises 2.30–2.32 | tree mapping 与 subsets |
2.2.2 Hierarchical Structures
用 lists 表示 sequences 的方式,自然可以 generalize 到表示 elements 本身可能是 sequences 的 sequences。例如,可以把 (cons (list 1 2) (list 3 4)) 构造出的 object ((1 2) 3 4) 看作一个包含 three items 的 list,其中第一个 item 自身是 list (1 2)。实际上,interpreter 打印 result 的形式已经暗示了这一点。Figure 2.5 展示了这个 structure 的 pair representation。

Figure 2.5: (cons (list 1 2) (list 3 4)) 形成的 structure。
也可以把 elements 是 sequences 的 sequences 看作 trees。Sequence 的 elements 是 tree 的 branches,而自身是 sequences 的 elements 则是 subtrees。Figure 2.6 把 Figure 2.5 中的 structure 作为 tree 展示。

Figure 2.6: Figure 2.5 中的 list structure 作为 tree 的 view。
Recursion 是处理 tree structures 的 natural tool,因为我们经常可以把 tree 上的 operations reduce 为 branches 上的 operations;这些 operations 又可以 reduce 为 branches 的 branches 上的 operations,如此继续,直到到达 tree 的 leaves。
例如,比较 §2.2.1 中的 length procedure 与 count-leaves procedure,后者返回一棵 tree 的 total number of leaves:
(define x (cons (list 1 2) (list 3 4)))
(length x)
3
(count-leaves x)
4
(list x x)
(((1 2) 3 4) ((1 2) 3 4))
(length (list x x))
2
(count-leaves (list x x))
8
要实现 count-leaves,回忆计算 length 的 recursive plan:
- List
x的length是 1 加上x的cdr的length。 - Empty list 的
length是 0。
count-leaves 类似。Empty list 的 value 也相同:
- Empty list 的
count-leaves是 0。
但在 reduction step 中,当我们 strip off list 的 car 时,必须考虑 car 自身可能是一个 tree,其 leaves 也需要 count。因此,合适的 reduction step 是:
- Tree
x的count-leaves,是x的car的count-leaves加上x的cdr的count-leaves。
最后,通过不断取 car 会到达 actual leaves,因此还需要另一个 base case:
- Leaf 的
count-leaves是 1。
为了帮助编写 tree 上的 recursive procedures,Scheme 提供了 primitive predicate pair?,用于测试 argument 是否为 pair。完整 procedure 如下:1
(define (count-leaves x)
(cond ((null? x) 0)
((not (pair? x)) 1)
(else (+ (count-leaves (car x))
(count-leaves (cdr x))))))
Exercises 2.24–2.29
Exercise 2.24. 假设 evaluate expression (list 1 (list 2 (list 3 4)))。给出 interpreter 打印的 result、对应的 box-and-pointer structure,以及把它解释为 tree 的结果(如 Figure 2.6)。
Exercise 2.25. 对下面每个 lists,给出能够从中 pick 出 7 的 car 与 cdr combinations:
(1 3 (5 7) 9)
((7))
(1 (2 (3 (4 (5 (6 7))))))
Exercise 2.26. 假设定义 x 和 y 为两个 lists:
(define x (list 1 2 3))
(define y (list 4 5 6))
对下面每个 expressions 求值时,interpreter 会打印什么 result?
(append x y)
(cons x y)
(list x y)
Exercise 2.27. 修改 Exercise 2.18 的 reverse procedure,产生一个 deep-reverse procedure。它接收一个 list,并返回一个 elements reversed、且所有 sublists 也 deep-reversed 的 list。例如:
(define x (list (list 1 2) (list 3 4)))
x
((1 2) (3 4))
(reverse x)
((3 4) (1 2))
(deep-reverse x)
((4 3) (2 1))
Exercise 2.28. 编写 procedure fringe,它接收一棵 tree(表示为 list),返回一个 list;该 list 的 elements 是 tree 的所有 leaves,按 left-to-right order 排列。例如:
(define x (list (list 1 2) (list 3 4)))
(fringe x)
(1 2 3 4)
(fringe (list x x))
(1 2 3 4 1 2 3 4)
Exercise 2.29. Binary mobile 由两个 branches 组成:left branch 和 right branch。每个 branch 是一根具有某个 length 的 rod,rod 上悬挂的可以是 weight,也可以是另一个 binary mobile。可以用两个 branches 构造 compound-data representation(例如使用 list):
(define (make-mobile left right)
(list left right))
Branch 由一个 length(必须是 number)和一个 structure 构造;structure 可以是表示 simple weight 的 number,也可以是另一个 mobile:
(define (make-branch length structure)
(list length structure))
a. 编写对应的 selectors left-branch、right-branch,返回 mobile 的 branches;以及 branch-length、branch-structure,返回 branch 的 components。
b. 使用 selectors 定义 total-weight procedure,返回 mobile 的 total weight。
c. 如果 mobile 的 top-left branch 施加的 torque 等于 top-right branch 施加的 torque(即 left rod length 与该 rod 上悬挂的 weight 的 product,等于 right side 的对应 product),并且悬挂在 branches 上的每个 submobile 都 balanced,则称 mobile 是 balanced。设计一个 predicate,测试 binary mobile 是否 balanced。
d. 假设把 mobile representation 改成下面的 constructors:
(define (make-mobile left right)
(cons left right))
(define (make-branch length structure)
(cons length structure))
为了转换到 new representation,需要修改 programs 的多少部分?
Mapping over trees
正如 map 是处理 sequences 的 powerful abstraction,map 与 recursion 结合起来,也是处理 trees 的 powerful abstraction。例如,scale-tree procedure 类似 §2.2.1 的 scale-list:它接收 numeric factor 和一棵 leaves 为 numbers 的 tree,返回一棵 shape 相同的 tree,其中每个 number 都乘以该 factor。
scale-tree 的 recursive plan 与 count-leaves 的 plan 类似:
(define (scale-tree tree factor)
(cond ((null? tree) nil)
((not (pair? tree)) (* tree factor))
(else (cons (scale-tree (car tree) factor)
(scale-tree (cdr tree) factor)))))
(scale-tree (list 1 (list 2 (list 3 4) 5) (list 6 7))
10)
(10 (20 (30 40) 50) (60 70))
实现 scale-tree 的另一种方式,是把 tree 看作 subtrees 的 sequence,并使用 map。我们遍历这个 sequence,依次 scale 每个 subtree,并返回 results list。在 base case 中,如果 tree 是 leaf,就直接把它乘以 factor:
(define (scale-tree tree factor)
(map (lambda (sub-tree)
(if (pair? sub-tree)
(scale-tree sub-tree factor)
(* sub-tree factor)))
tree))
许多 tree operations 都可以用类似的 sequence operations 与 recursion 的 combinations 实现。
Exercises 2.30–2.32
Exercise 2.30. 定义一个类似 Exercise 2.21 的 square-tree procedure。也就是说,square-tree 应当如下工作:
(square-tree
(list 1
(list 2 (list 3 4) 5)
(list 6 7)))
(1 (4 (9 16) 25) (36 49))
分别直接定义 square-tree(即不使用 higher-order procedures),以及使用 map 和 recursion 定义它。
Exercise 2.31. 把 Exercise 2.30 的 answer abstract 出来,产生具有如下 property 的 tree-map procedure,使得 square-tree 可以这样定义:
(define (square-tree tree) (tree-map square tree))
Exercise 2.32. 可以把 set 表示为 distinct elements 的 list,把一个 set 的所有 subsets 表示为 lists 的 list。例如,如果 set 是 (1 2 3),那么所有 subsets 是 (() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3))。补全下面这个生成 set subsets 的 procedure,并清楚解释它为什么 work:
(define (subsets s)
(if (null? s)
(list nil)
(let ((rest (subsets (cdr s))))
(append rest (map <??> rest)))))
图与格式复核
- 保留 Figure 2.5 与 Figure 2.6 的 2 个原始 GIF,按原文位置嵌入并保留显示尺寸。
count-leaves、scale-tree的两种实现、mobile constructors 与 Exercises 2.24–2.32 的 code/题号顺序保持不变。
术语对照
| English | 译法/处理 |
|---|---|
| hierarchical structure | 保留 hierarchical structure;必要处译作层次结构 |
| tree / subtree / leaf | 保留 tree、subtree、leaf;必要处译作树、子树、叶 |
| branch | 保留 branch;必要处译作分支 |
| mobile / torque | 保留 mobile、torque;必要处译作活动挂架、力矩 |
| deep-reverse | 保留 deep-reverse |
| fringe | 保留 fringe;必要处译作边缘 |
| mapping over trees | 保留 mapping;必要处译作在树上进行映射 |
参考资料
-
这里的
pair?是用于判断 argument 是否为 pair 的 primitive predicate;它使 recursive procedure 能在 leaves 处停止。 ↩
评论 (0)