SICP:1.3.3 Procedures as General Methods
SICP:1.3.3 Procedures as General Methods
本文逐段翻译 SICP 第二版 §1.3.3《Procedures as General Methods》,保留 half-interval method、fixed-point search、average damping、公式图件以及 Exercises 1.35–1.39 的原文顺序。
TL;DR
- Higher-order procedures 不仅能抽象具体 operations,还能表达 independent of particular functions 的 general methods of computation。
- Half-interval method 通过不断二分 sign-changing interval 寻找 equation 的 root;fixed-point method 通过反复应用 function 寻找
f(x) = x的 point。 - Square-root computation 可以改写为 fixed-point search;直接迭代
x/y会 oscillate,使用 average damping 后收敛。
目录
| 章节 | 说明 |
|---|---|
| 1.3.3 Procedures as General Methods | general computation methods |
| Half-interval method | 寻找 equations 的 roots |
| Fixed-point method | 寻找 functions 的 fixed points |
| Average damping | 让 fixed-point search 收敛 |
| Exercises 1.35–1.39 | continued fractions 与相关 methods |
1.3.3 Procedures as General Methods
我们在 §1.1.4 引入 compound procedures,作为一种 abstraction mechanism,使 numerical operations 的 patterns independent of particular numbers。借助 §1.3.1 的 higher-order procedure integral,我们开始看到一种更 powerful 的 abstraction:用 procedures 表达 independent of particular functions 的 general methods of computation。
本节讨论两个更 elaborate 的 examples——寻找 functions 的 zeros 和 fixed points 的 general methods,并展示如何直接把这些 methods 表达成 procedures。
Half-interval method
Half-interval method 是寻找 equation f(x) = 0 的 roots 的 simple but powerful technique,其中 f 是 continuous function。其 idea 是:如果给定 points a 与 b,满足 f(a) < 0 < f(b),那么 f 在 a 与 b 之间必定至少有一个 zero。
要 locate 一个 zero,令 x 为 a 与 b 的 average,并计算 f(x)。如果 f(x) > 0,那么 f 必定在 a 与 x 之间有一个 zero;如果 f(x) < 0,那么 f 必定在 x 与 b 之间有一个 zero。继续这样做,就能识别出越来越小的 intervals,在这些 intervals 上 f 必定有一个 zero。当 interval 足够 small 时,process 停止。
由于 uncertainty interval 在每个 step 都缩小一半,所需 steps 数量的 growth 是 Θ(log(L/T)),其中 L 是 original interval 的 length,T 是 error tolerance(即我们认为“small enough”的 interval size)。下面的 procedure 实现了这个 strategy:
(define (search f neg-point pos-point)
(let ((midpoint (average neg-point pos-point)))
(if (close-enough? neg-point pos-point)
midpoint
(let ((test-value (f midpoint)))
(cond ((positive? test-value)
(search f neg-point midpoint))
((negative? test-value)
(search f midpoint pos-point))
(else midpoint))))))
假设一开始给定 function f,以及其 values 分别为 negative 和 positive 的 points。先计算两 given points 的 midpoint;接着检查给定 interval 是否 small enough,如果是,就直接返回 midpoint 作为 answer。否则,把 f 在 midpoint 处的 value 作为 test value。如果 test value positive,就用 original negative point 到 midpoint 的新 interval 继续 process;如果 test value negative,就用 midpoint 到 positive point 的 interval 继续 process。最后还有一种可能:test value 为 0,此时 midpoint 自身就是所 search 的 root。
可以使用与 §1.1.7 计算 square roots 时类似的 procedure,测试 endpoints 是否“close enough”1:
(define (close-enough? x y)
(< (abs (- x y)) 0.001))
直接使用 search 很 awkward,因为可能意外地给出 f values 没有所需 signs 的 points,从而得到 wrong answer。因此,使用下面的 procedure 间接调用 search:它检查两个 endpoints 哪个 function value negative、哪个 positive,并相应调用 search。如果 function 在两个 given points 上具有相同 sign,就无法使用 half-interval method,此时 procedure signals an error2。
(define (half-interval-method f a b)
(let ((a-value (f a))
(b-value (f b)))
(cond ((and (negative? a-value) (positive? b-value))
(search f a b))
((and (negative? b-value) (positive? a-value))
(search f b a))
(else
(error "Values are not of opposite sign" a b)))))
下面的 example 使用 half-interval method,把 sin x = 0 在 2 与 4 之间的 root 作为
的 approximation:
(half-interval-method sin 2.0 4.0)
3.14111328125
另一个 example 使用 half-interval method,在 1 与 2 之间 search equation x³ - 2x - 3 = 0 的 root:
(half-interval-method (lambda (x) (- (* x x x) (* 2 x) 3))
1.0
2.0)
1.89306640625
Fixed-point method
如果 number x 满足 equation f(x) = x,就称 x 是 function f 的 fixed point。对于一些 functions f,可以从 initial guess 开始,反复应用 f:

直到 value 不再发生太大变化。利用这个 idea,可以设计 fixed-point procedure:它接受一个 function 和 initial guess 作为 inputs,返回该 function 的 fixed point approximation。它反复应用 function,直到找到两个 successive values,其 difference 小于 prescribed tolerance:
(define tolerance 0.00001)
(define (fixed-point f first-guess)
(define (close-enough? v1 v2)
(< (abs (- v1 v2)) tolerance))
(define (try guess)
(let ((next (f guess)))
(if (close-enough? guess next)
next
(try next))))
(try first-guess))
例如,可以从 1 这个 initial approximation 开始,使用该 method 计算 cosine function 的 fixed point3:
(fixed-point cos 1.0)
.7390822985224023
同样,也可以找到 equation y = sin y + cos y 的 solution:
(fixed-point (lambda (y) (+ (sin y) (cos y)))
1.0)
1.2587315962971173
Average damping
Fixed-point process 让人联想到 §1.1.7 中寻找 square roots 的 process。两者都建立在以下 idea 上:不断 improve guess,直到 result 满足某个 criterion。
实际上,可以很容易地把 square-root computation formulate 为 fixed-point search。计算某个 number x 的 square root,需要寻找满足 y² = x 的 y。把这个 equation 改写成 equivalent form y = x/y,可以看出我们是在寻找 function y → x/y 的 fixed point4,因此可以尝试这样计算 square roots:
(define (sqrt x)
(fixed-point (lambda (y) (/ x y))
1.0))
不幸的是,这个 fixed-point search 不会 converge。考虑一个 initial guess y₁:next guess 是 y₂ = x/y₁,再下一个 guess 是 y₃ = x/y₂ = x/(x/y₁) = y₁。于是会出现 infinite loop,两个 guesses y₁ 与 y₂ 不断重复,围绕 answer oscillate。
控制这种 oscillation 的一种方式,是 prevent guesses 变化过大。Answer 总是在 guess y 与 x/y 之间,因此可以将 y 与 x/y 求 average,得到一个没有 x/y 那么远离 y 的 new guess。这样,y 后的 next guess 是 (1/2)(y + x/y),而不是 x/y。
构造这种 guesses sequence,正是寻找 function y → (1/2)(y + x/y) fixed point 的 process:
(define (sqrt x)
(fixed-point (lambda (y) (average y (/ x y)))
1.0))
(注意,y = (1/2)(y + x/y) 是 equation y = x/y 的 simple transformation:将 equation 两边都加上 y,再除以 2。)
经过这个 modification,square-root procedure 就能工作了。事实上,展开这些 definitions 可以看到,这里生成的 square-root approximations sequence,恰好与 §1.1.7 原始 square-root procedure 生成的 sequence 相同。这种对 successive approximations 求 average 的 approach,称为 average damping,经常有助于 fixed-point searches 的 convergence。
Exercises 1.35–1.39
Exercise 1.35
证明 golden ratio
(§1.2.2)是 transformation x → 1 + 1/x 的 fixed point,并使用 fixed-point procedure 计算
。
Exercise 1.36
修改 fixed-point,使它使用 Exercise 1.22 中展示的 newline 和 display primitives,打印它生成的 approximations sequence。然后通过寻找 transformation x → log(1000) / log(x) 的 fixed point,求解 xˣ = 1000。使用 Scheme 的 primitive log(计算 natural logarithms)。比较使用和不使用 average damping 时所需的 steps 数量。(注意,不能以 1 作为 fixed-point 的 initial guess,因为这会导致除以 log(1) = 0。)
Exercise 1.37
a. Infinite continued fraction 是以下形式的 expression:

例如,可以证明,当所有 Nᵢ 和 Dᵢ 都等于 1 时,infinite continued fraction expansion 产生 1/φ,其中 φ 是 golden ratio。approximate infinite continued fraction 的一种方法,是在指定数量的 terms 后 truncate expansion。这样的 truncation——所谓 k-term finite continued fraction——形式如下:

假设 n 和 d 是 single-argument procedures;它们接受 term index i,返回 continued fraction terms 的 Nᵢ 和 Dᵢ。定义 cont-frac,使 (cont-frac n d k) 计算 k-term finite continued fraction 的 value。使用以下方式逐渐增加 k,以 approximation 1/φ 检查你的 procedure:
(cont-frac (lambda (i) 1.0)
(lambda (i) 1.0)
k)
要得到 accurate 到 4 decimal places 的 approximation,k 必须多大?
b. 如果你的 cont-frac procedure 生成 recursive process,就写一个生成 iterative process 的 version;如果它生成 iterative process,就写一个生成 recursive process 的 version。
Exercise 1.38
1737 年,Swiss mathematician Leonhard Euler 在 memoir《De Fractionibus Continuis》中发表了 e - 2 的 continued fraction expansion,其中 e 是 natural logarithms 的 base。在这个 fraction 中,所有 Nᵢ 都是 1,Dᵢ 依次为 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, ...。编写一个使用 Exercise 1.37 的 cont-frac、依据 Euler expansion approximation e 的 program。
Exercise 1.39
German mathematician J. H. Lambert 于 1770 年发表了 tangent function 的 continued fraction representation:

其中 x 以 radians 表示。定义 (tan-cf x k) procedure,依据 Lambert formula 计算 tangent function 的 approximation。k 指定要计算的 terms 数量,和 Exercise 1.37 一样。
图与格式复核
- 保留 half-interval、fixed-point、continued-fraction 与 tangent formula 的 8 个原始图件,并按原文位置嵌入。
search、half-interval-method、fixed-point、average damping 与 Exercises 1.35–1.39 的 code/题干顺序保持不变。
术语对照
| English | 译法/处理 |
|---|---|
| general method | 保留 general method;必要处译作一般方法 |
| half-interval method | 保留 half-interval method;必要处译作半区间法 |
| root / zero | 保留 root、zero;必要处译作根、零点 |
| fixed point | 保留 fixed point;必要处译作不动点 |
| convergence / oscillation | 保留 convergence、oscillation |
| average damping | 保留 average damping;必要处译作平均阻尼 |
| continued fraction | 保留 continued fraction;必要处译作连分式 |
参考资料
- Harold Abelson、Gerald Jay Sussman、Julie Sussman:SICP 第二版 MIT Press 页面
- SICP HTML full text
- MIT OCW 6.001 Readings
-
0.001只是代表性的“small” number,用于表示 calculation 可接受的 tolerance。Real calculation 中合适的 tolerance 取决于 problem、computer 和 algorithm 的 limitations,这往往是 subtle 的 consideration。 ↩ -
可以使用
error实现 signal;它接受若干 items 作为 arguments,并将它们打印为 error messages。 ↩ -
原文建议在无聊的 lecture 中把 calculator 调到 radians mode,反复按
cosbutton,直到得到 fixed point。 ↩ -
数学家用
→(读作“maps to”)表示lambda。y → x/y就是(lambda (y) (/ x y)),即 value aty为x/y的 function。 ↩
评论 (0)