SML# Document Version 3.7.1
7 Introduction to ML programming

7.8 Functions with multiple arguments

The function power defined in the previous section computers the power Cn of C for a given C. There are two ways of generalizing this function so that it takes both n and C and return Cn. They are shown in the following interactive session.

# fun powerUncurry (0, C) = 1
>   | powerUncurry (n, C) = C * powerUncurry (n - 1, C);
val powerUncurry = fn : int * int -> int
# powerUncurry (2,3);
val it = 9 : int
 
# fun powerCurry 0 C = 1
>   | powerCurry n C = C * (powerCurry (n - 1) C);
val powerCurry = fn : int -> int -> int
# powerCurry 2 3;
val it = 9 : int

The type int * int -> int of powerUncurry indicates that it is a function that takes a pair of integers and returns an integer. In contrast, the type int -> int -> int of powerCurry says that it is a function that takes an integer and returns a function of type int -> int. As seen in these example, in ML, a function is defined in the format as it will be used. For example, powerUncurry is defined fun powerUncurry (C,n) = ... so it is used as powerUncurry (2, 3).