We have seen that procedures are, in effect, abstractions that describe
compound operations on numbers independent of the particular numbers. For
example, when we
我们已经看到,过程实际上是一种抽象——它描述对数的复合运算,而与具体的数无关。例如,当我们
we are not talking about the cube of a particular number, but rather about a
method for obtaining the cube of any number. Of course we could get along
without ever defining this procedure, by always writing expressions such as
我们谈论的不是某个特定数的立方,而是一种求任意数之立方的方法。当然,若从不定义这个过程,而始终写出如下形式的表达式,我们也能应付,
and never mentioning cube explicitly. This would place us at a serious
disadvantage, forcing us to work always at the level of the particular
operations that happen to be primitives in the language (multiplication, in
this case) rather than in terms of higher-level operations. Our programs would
be able to compute cubes, but our language would lack the ability to express
the concept of cubing. One of the things we should demand from a powerful
programming language is the ability to build abstractions by assigning names to
common patterns and then to work in terms of the abstractions directly.
Procedures provide this ability. This is why all but the most primitive
programming languages include mechanisms for defining procedures.
从不显式提及 cube。但这会使我们处于严重的不利地位,迫使我们始终在语言中恰好是基本元素的那些具体运算的层次上工作(本例中为乘法),而无法在更高层次的运算上工作。我们的程序固然能计算立方,但我们的语言将缺乏表达“立方”这一概念的能力。我们对一门强大的程序设计语言应有的要求之一,就是能够通过给常见模式命名来构造抽象,进而直接在抽象的层次上工作。过程提供了这种能力,这也正是为何除了最基本的程序设计语言之外,所有语言都包含定义过程的机制。
Yet even in numerical processing we will be severely limited in our ability to
create abstractions if we are restricted to procedures whose parameters must be
numbers. Often the same programming pattern will be used with a number of
different procedures. To express such patterns as concepts, we will need to
construct procedures that can accept procedures as arguments or return
procedures as values. Procedures that manipulate procedures are called
然而,即使在数值处理中,如果我们的过程参数只能是数,创建抽象的能力也将大受限制。同一种程序设计模式往往会被用于若干不同的过程。要将这类模式表达为概念,就需要构造能够接受过程作为参数、或将过程作为值返回的过程。操纵过程的过程被称为
higher-order procedures. This section shows how higher-order
procedures can serve as powerful abstraction mechanisms, vastly increasing the
expressive power of our language.
高阶过程 (higher-order procedures)。本节将说明高阶过程如何充当强大的抽象机制,极大地增强我们语言的表达能力。
(define (cube x) (* x x x)) (* 3 3 3)
(* x x x)
(* y y y)