We have seen that
functions
are, in effect, abstractions that describe compound operations on
numbers independent of the particular numbers. For example, when we
declare
これまで見てきたように、
関数
とは、特定の数値に依存しない、数値に対する複合的な操作を記述する抽象です。例えば、次のように
宣言
するとき
function cube(x) {
return x * x * x;
}
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
declaring this function,
by always writing expressions such as
私たちは特定の数の3乗について話しているのではなく、任意の数の3乗を求める方法について話しています。もちろん、この
関数を宣言
しなくても、常に次のような式を書けばやっていけるでしょう。
3 * 3 * 3
x * x * x
y * y * y
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.
Functions
provide this ability. This is why all but the most primitive
programming languages include mechanisms for
declaring functions.
そうすれば cube に明示的に言及する必要はありません。しかし、これでは重大な不利を被ることになります。言語のプリミティブである特定の操作(この場合は乗算)のレベルで常に作業せざるを得なくなり、より高レベルの操作で考えることができなくなるからです。プログラムは3乗を計算できても、「3乗する」という概念を表現する能力が言語に欠けてしまいます。強力なプログラミング言語に求めるべきことの一つは、共通のパターンに名前を付けて抽象を構築し、その抽象を直接使って作業できる能力です。
関数
はこの能力を提供します。だからこそ、最もプリミティブな言語を除くすべてのプログラミング言語が、
関数を宣言する
メカニズムを備えているのです。
Yet even in numerical processing we will be severely limited in our
ability to create abstractions if we are restricted to
functions
whose parameters must be numbers. Often the same programming pattern
will be used with a number of different
functions.
To express such patterns as concepts, we will need to construct
functions
that can accept
functions
as arguments or return
functions
as values.
Functions
that manipulate
functions
are called
higher-order functions.
This section shows how higher-order
functions
can serve as powerful abstraction mechanisms, vastly increasing the
expressive power of our language.
しかし、数値処理においてさえ、パラメータが数値でなければならない
関数
に制限されていては、抽象を作る能力は大きく制限されてしまいます。同じプログラミングパターンが、多くの異なる
関数
で使われることがよくあります。そのようなパターンを概念として表現するには、
関数
を引数として受け取ったり、
関数
を値として返したりできる
関数
を構築する必要があります。
関数
を操作する
関数
は、
高階関数と呼ばれます。このセクションでは、高階
関数
が強力な抽象化メカニズムとして機能し、言語の表現力を大幅に高めることを示します。