Next Previous Contents

3. Functions

This covers sections 3.1 and 3.2 in Ullman.

We shall begin with an example:

> fun add(x,y) = x + y;
val add = fn: int * int -> int

This defines the function add which adds two integers. The response line indicates that this function is a mapping from int * int tuples to ints. The interpreter inferred from the function body that the parameter is an int * int, even though no parameter types were specified.

The keyword fun defines a function, and the word fn, also a keyword, specifies a value having a ``function'' type.

A function is invoked by providing the function name followed by its arguments, or () if the function has no parameters. The empty parameter list () is also used to specify an empty parameter list when defining a function (this isn't introduced in the text until section 4.1 in Ullman). An example of a function:

> add(3, 5);
val it = 8 : int

It is also possible to specify a type of a value explicitly:

> fun square(x:real) = x * x;
val square = fn: real -> real

If :real were not specified, the interpreter would assume that ints were to be used. The function could also be specified in these ways:

> fun square(x) = (x:real) * x;
> fun square(x) = x * (x:real);

There are no constructs for looping (this is a lie: we will discuss this later). Iteration is achieved by recursion. Example:

> fun reverse( L ) =
=    if L = nil then nil
=    else reverse( tl( L ) ) @ [hd( L )];
val reverse = fn: 'a list -> 'a list

Since there are no type-specific operators in this function, ML uses the anonymous type specification 'a.


Next Previous Contents