灯下 登录
计算机科学 / SICP / 1.3.1 Procedures as Arguments

Exercise 1.32 · 习题

Exercise 1.32:

Show that sum and product (Exercise 1.31) are both special
cases of a still more general notion called accumulate that combines a
collection of terms, using some general accumulation function:

(accumulate
combiner null-value term a next b)

Accumulate takes as arguments the same term and range specifications as
sum and product, together with a combiner procedure (of
two arguments) that specifies how the current term is to be combined with the
accumulation of the preceding terms and a null-value that specifies what
base value to use when the terms run out. Write accumulate and show how
sum and product can both be defined as simple calls to
accumulate.

If your accumulate procedure generates a recursive process, write one

that generates an iterative process. If it generates an iterative process,

write one that generates a recursive process.

练习 1.32:

证明 sum 和 product(练习 1.31)都是一个称为 accumulate 的更一般概念的特殊情形,accumulate 用某个通用的累积函数来组合一组项:

(accumulate
combiner null-value term a next b)

accumulate 接受与 sum 和 product 相同的项与范围规格,还接受一个两参数的 combiner 过程(指定当前项如何与前面各项的累积结果相组合)以及一个 null-value(指定当项耗尽时使用的基准值)。写出 accumulate,并说明如何将 sum 和 product 都定义为对 accumulate 的简单调用。

若你的 accumulate 过程产生递归型计算过程,请再写一个产生迭代型计算过程的版本;若它产生迭代型计算过程,则写一个产生递归型计算过程的版本。