目录
正在加载目录…
正在阅读专栏 SICP:Structure and Interpretation of Computer Programs 第 15 篇 · 共 80 篇 · 19%

SICP:1.3.1 Procedures as Arguments

发布于 2026-09-18 08:12 · 最后编辑于 2026-09-18 08:50 · 版本 v2 · 字数 5,015 · 预计阅读 11 分钟 👁 15 次阅读

SICP:1.3.1 Procedures as Arguments

本文逐段翻译 SICP 第二版 §1.3.1《Procedures as Arguments》,保留 summation abstraction、π approximation、definite integral、Scheme code、公式图件以及 Exercises 1.29–1.33 的原文顺序。

TL;DR

  • sum-integers、sum-cubes 和 pi-sum 共享同一个 computation pattern;把 pattern 的 slots 提升为 formal parameters,就得到 higher-order procedure sum。
  • Procedure 可以作为 argument 传给 procedure;这使 programming language 能表达“summation itself”等更高层 abstraction,而不只是某个具体 sum。
  • sum 可以作为 building block 组合出 factorial/product、integral、filtered-accumulate 等更一般的 abstraction。

目录

章节说明
1.3.1 Procedures as Argumentshigher-order procedure 的引入
Summation abstraction从 common template 到 sum
Using sumcubes、integers 与 π
Definite integral用 sum 作为 numerical building block
Exercises 1.29–1.33Simpson、product 与 accumulate

1.3.1 Procedures as Arguments

考虑以下三个 procedures。第一个计算从 a 到 b 的 integers 之和:

scheme
(define (sum-integers a b)
  (if (> a b)
      0
      (+ a (sum-integers (+ a 1) b))))

第二个计算给定 range 中 integers 的 cubes 之和:

scheme
(define (sum-cubes a b)
  (if (> a b)
      0
      (+ (cube a) (sum-cubes (+ a 1) b))))

第三个计算以下 series 中一组 terms 的 sum:

ch1 Z G 26

这个 series(非常 slowly)converges to book Z G D 9/81:

scheme
(define (pi-sum a b)
  (if (> a b)
      0
      (+ (/ 1.0 (* a (+ a 2)))
         (pi-sum (+ a 4) b))))

这三个 procedures 显然 share 一个 common underlying pattern。它们在大部分地方 identical,区别只在于 procedure 的 name、用来计算待加 term 的 a 的 function,以及提供 a 下一个 value 的 function。我们可以通过在同一个 template 中填充 slots,生成这三个 procedures:

scheme
(define (<name> a b)
  (if (> a b)
      0
      (+ (<term> a)
         (<name> (<next> a) b))))

Summation abstraction

存在这样一个 common pattern,强烈说明有一个 useful abstraction 正等待被 brought to the surface。事实上,mathematicians 很早就识别出了 summation of a series 这一 abstraction,并发明了 sigma notation,例如:

ch1 Z G 27

用来表达这个 concept。Sigma notation 的 power 在于,它允许 mathematicians 处理 summation 本身,而不是只处理 particular sums——例如,可以 formulate 与被 sum 的 particular series 无关的 general results。

同样,作为 program designers,我们希望 language 足够 powerful,使我们能够写出表达 summation itself 的 procedure,而不只是计算 particular sums 的 procedures。利用上面的 common template,并把其中的 slots transform 成 formal parameters,我们可以很容易在 procedural language 中做到这一点:

scheme
(define (sum term a next b)
  (if (> a b)
      0
      (+ (term a)
         (sum term (next a) next b))))

注意,sum 的 arguments 包括 lower and upper bounds a、b,以及 procedures term 和 next。

Using sum

我们可以像使用任何 procedure 一样使用 sum。例如,可以结合一个把 argument increment 1 的 procedure inc,定义 sum-cubes:

scheme
(define (inc n) (+ n 1))

(define (sum-cubes a b)
  (sum cube a inc b))

借助这个 definition,可以计算从 1 到 10 的 integers 的 cubes 之和:

scheme
(sum-cubes 1 10)
3025

借助计算 term 的 identity procedure,也可以用 sum 定义 sum-integers:

scheme
(define (identity x) x)

(define (sum-integers a b)
  (sum identity a inc b))

于是可以把从 1 到 10 的 integers 加起来:

scheme
(sum-integers 1 10)
55

还可以用相同的方式定义 pi-sum2:

scheme
(define (pi-sum a b)
  (define (pi-term x)
    (/ 1.0 (* x (+ x 2))))
  (define (pi-next x)
    (+ x 4))
  (sum pi-term a pi-next b))

使用这些 procedures,可以计算 book Z G D 9 的 approximation:

scheme
(* 8 (pi-sum 1 1000))
3.139592655589783

Definite integral

有了 sum,就可以把它作为 building block,进一步 formulate concepts。例如,function f 在 limits a 与 b 之间的 definite integral,当 dx 很小时,可以使用以下 formula 做 numerical approximation:

ch1 Z G 28

可以直接把它表达成 procedure:

scheme
(define (integral f a b dx)
  (define (add-dx x) (+ x dx))
  (* (sum f (+ a (/ dx 2.0)) add-dx b)
     dx))

(integral cube 0 1 0.01)
.24998750000000042

(integral cube 0 1 0.001)
.249999875000001

(cube 在 0 和 1 之间的 integral 的 exact value 是 1/4。)

Exercises 1.29–1.33

Exercise 1.29

Simpson's Rule 是比上面展示的 method 更 accurate 的 numerical integration method。使用 Simpson's Rule,function f 在 a 与 b 之间的 integral 可以近似为:

ch1 Z G 29

其中 h = (b - a) / n,n 是某个 even integer,yₖ = f(a + kh)。(增加 n 会增加 approximation 的 accuracy。)定义一个 procedure,接受 f、a、b 和 n 作为 arguments,并返回使用 Simpson's Rule 计算的 integral value。使用你的 procedure,计算 cube 在 0 与 1 之间的 integral(分别令 n = 100 和 n = 1000),并将 results 与上面的 integral procedure 的 results 比较。

Exercise 1.30

上面的 sum procedure 生成 linear recursion。可以把它重写为 iterative 方式执行 sum。通过补全下面 definition 中缺失的 expressions,展示如何做到这一点:

scheme
(define (sum term a next b)
  (define (iter a result)
    (if <??>
        <??>
        (iter <??> <??>)))
  (iter <??> <??>))

Exercise 1.31

a. sum procedure 只是大量类似 abstraction 中最 simple 的一个;这些 abstractions 都可以作为 higher-order procedures 捕获3。写一个 analogous 的 product procedure,返回给定 range 中 function 在各 points 上的 values 的 product。展示如何用 product 定义 factorial。还要使用 product,按照下面的 formula 计算 book Z G D 9 的 approximations4:

ch1 Z G 30

b. 如果你的 product procedure 生成 recursive process,就写一个生成 iterative process 的 version;如果它生成 iterative process,就写一个生成 recursive process 的 version。

Exercise 1.32

a. 展示 sum 和 product(Exercise 1.31)都是一个更 general notion accumulate 的 special cases;accumulate 使用某个 general accumulation function 组合一组 terms:

scheme
(accumulate combiner null-value term a next b)

accumulate 接受与 sum、product 相同的 term 和 range specifications,同时接受一个两个 arguments 的 combiner procedure;它指定 current term 如何与 preceding terms 的 accumulation 组合;还接受一个 null-value,指定 terms 用尽时使用的 base value。写出 accumulate,并展示如何把 sum 和 product 都定义为对 accumulate 的 simple calls。

b. 如果你的 accumulate procedure 生成 recursive process,就写一个生成 iterative process 的 version;如果它生成 iterative process,就写一个生成 recursive process 的 version。

Exercise 1.33

通过引入对待组合 terms 的 filter notion,可以得到一个更 general 的 accumulate version(Exercise 1.32)。也就是说,只组合 range 中 derived values 满足指定 condition 的 terms。得到的 filtered-accumulate abstraction 接受与 accumulate 相同的 arguments,另外再接受一个 single-argument predicate,指定 filter。将 filtered-accumulate 写成 procedure,并展示如何用它表达:

a. interval a 到 b 中 prime numbers 的 squares 之和(假设已经写好了 prime? predicate);

b. 小于 n 的所有 positive integers 中与 n relatively prime 的 integers 的 product(即所有满足 i < n 且 GCD(i, n) = 1 的 positive integers)。

图与格式复核

  • 保留 summation series、sigma notation、integral、Simpson's Rule、product approximation 等 6 个原始公式 GIF,并按原文位置嵌入。
  • sum、pi-sum、integral、product、accumulate、filtered-accumulate 与 Exercises 1.29–1.33 的顺序保持不变。

术语对照

English译法/处理
higher-order procedure保留 higher-order procedure;必要处译作高阶 procedure
summation of a series保留 summation;必要处译作级数求和
sigma notation保留 sigma notation;必要处译作 sigma 表示法
term / next保留 term、next
definite integral保留 definite integral;必要处译作定积分
Simpson's Rule保留 Simpson's Rule;必要处译作辛普森法则
product保留 product;必要处译作乘积
accumulate保留 accumulate;必要处译作累积
filter / filtered-accumulate保留 filter、filtered-accumulate

参考资料

  1. 这个 series 通常写成 equivalent form π/4 = 1 - 1/3 + 1/5 - 1/7 + ...,由 Leibniz 提出;Chapter 3 将展示如何以此为基础进行 numerical tricks。 ↩

  2. 注意,我们使用了 §1.1.8 的 block structure,把 pi-next 与 pi-term 的 definitions embed 到 pi-sum 中,因为这些 procedures 不太可能有其他用途;§1.3.2 将展示如何完全去掉它们。 ↩

  3. Exercises 1.31–1.33 的 intent,是展示适当 abstraction 如何 consolidate 许多看似 disparate operations 后获得的 expressive power。不过,在目前还没有 data structures、无法为这些 abstractions 提供合适 combination means 的情况下,使用 accumulation 和 filtering 仍受到一定限制;§2.2.3 将回到这些 ideas。 ↩

  4. 这个 formula 由 seventeenth-century English mathematician John Wallis 发现。 ↩

← 返回列表
← 上一篇 SICP:1.2.6 Example: Testing for Primality 下一篇 → SICP:1.3.2 Constructing Procedures Using Lambda

评论 (0)

暂无评论,来留下第一条吧。
登录 或 注册 后才能发表评论