A higher-order function is a function that take another function as
its argument. For instance, the following defined function
rtfind is a higher-order one:
Given a function from integers to integers,
rtfind searches
for the first natural number that is a root of the function. For instance,
calling
rtfind on the polynomial function
lam x => x * x
- x + 110 returns
11. Note that
rtfind
loops forever if it is applied to a function that does not have a root.
Higher-order functions can greatly facilitate code reuse, and I now
present a simple example to illustrate this point. The following defined
functions sum and prod compute the sum and
product of the integers ranging from 1 to a given natural number,
respectively:
The similarity between the functions
sum and
prod
is evident. We can define a higher-function
ifold and then
implement
sum and
prod based on
ifold:
If we ever want to compute the sum of the squares of the integers ranging
from 1 to a given natural number, we can readily define a function based on
ifold to do it:
As more features of ATS are introduced, higher-order functions will become
even more effective in facilitating code reuse.