SML# Document Version 3.7.1
7 Introduction to ML programming

7.10 Higher-order functions

As we learned that powerCurry 2 3 is interpreted as ((powerCurry 2) 3). This implies:

(powerCurry 2) is a function.

In ML, functions can be generated by a program. powerCurry is a program that takes an integer and return a function. Generated functions can be named and passed to other functions. For example, one can write the following code.

# val square = powerCurry 2;
val square = fn : int -> int
# square 3;
val it = 9 : int
# fun apply f = f 3;
val apply = fn : [’a. (int -> ’a) -> ’a]
# apply square;
val it = 9 : int

The type of apply means that this is a function that takes a function on integers and return an integer.

In summary, ML has the following power.

Just like integers, functions are values that can be returned from a function or passed to a function. In particular, a function can take a function as a parameter.

This is the important principle of ML under the fundamental principle of ML programming saying that programs are expressions. A function that takes a function as a parameter or return a function is called a higher-order function.