Some languages, like Pascal, support only a limited form of higher order. In Pascal for instance it is possible to define functions which has another functions as parameter, but the actual parameter must be the name of a function.
fun quicksort(nil) = nil | quicksort(h::t) = let val (low,high) = split(h,t) in append(quicksort(low), (h::quicksort(high))) end and split(x,nil) = (nil, nil) | split(x,(h::t)) = let val (k,m) = split(x,t) in if h < x then ((h::k),m) else (k,(h::m)) end;This function is of type int list -> int list, which means that can only be applied to lists of integers. However, the algorithm is very general, and one could easily imagine of applying the same method to order other kinds of lists, under the appropriate ordering relations. For instance: lists of strings under the lexicographic ordering, lists of lists under the ordering "shorter than", etc. The only thing that one needs to do is to replace the "<" relation in the test h < x in split with the appropriate ordering relation.
Of course, one would wish to write only one program, and to express the dependence on the ordering relation by using a parameter. Note however that the ordering relation is a function (its most general type is 'a * 'a -> bool), hence we need that the language allow expressing quicksort as an higher-order function. ML, being an Higher-order language, allows it, and we can define quicksort as a functions with two parameters, the list to be ordered and the ordering relation, as follows:
fun quicksort(nil,ord) = nil | quicksort(h::t,ord) = let val (low,high) = split(h,t,ord) in append(quicksort(low,ord), (h::quicksort(high,ord))) end and split(x,nil,ord) = (nil, nil) | split(x,(h::t),ord) = let val (k,m) = split(x,t,ord) in if ord(h,x) then ((h::k),m) else (k,(h::m)) end;The type of this definition of quicksort is 'a list * ('a list * 'a list -> bool) -> 'a list.
For instance, we can use quicksort with the orderings "<" and ">" on integers. The only problem is that they are infix operators, while the pameter ord is used as a prefix operator. This problem can be solved by using the predefined "op" function, which trasforms an infix operator into a prefix one:
- quicksort([3,2,5,1],op <); val it = [1,2,3,5] : int list - quicksort([3,2,5,1],op >); val it = [5,3,2,1] : int listLet us see some other examples of functions with higher-order parameters.
fun map(,f) =  | map(a::L,f) = (f a)::map(L,f);For example, is we have previously defined fact as the factorial function, we have:
- map([1,2,3,4],fact); val it = [1,2,6,24] : int listOther examples are:
- map([true,false,true],not); val it = [false,true,false] : bool list - map([(1,2),(3,4),(5,6)], op +); val it = [3,7,11] : int list
fun reduce(,f,v) = v | reduce(a::L,f,v) = f(a,reduce(L,f,v));Examples:
- reduce([1,2,3,4],op +,0); (* sum all the elements of the list) val it = 10 : int - reduce([1,2,3,4],op *,1); (* multiply all the elements of the list) val it = 24 : int - reduce([[1,2],,[5,6,7]],op @,); (* flatten the list) val it = [1,2,5,6,7] : int list
fn Id => Expwhere Id is a name representing the parameter of the function, and Exp is an expression representing the body of the function. For example:
fn x => x * 2;represents a function that, given a number n, returns the double of n, i.e. n * 2.
More in general, instead of a name as parameter we can use a pattern, like a tuple. For instance
fn (x,y) => if x < y then y - x else x - y;represents a binary function that, given two numbers n and m, returns the distance (namely the absolute value of the difference) between n and m.
Anonymous functions can be used anywhere a function of the same type is expected. For instance:
- map([1,2,3,4],fn x => x * 2); val it = [2,4,6,8] : int list - reduce([1,2,3,4],fn (x,y) => if x = 0 then true else y,false); val it = false : boolNote: the expression
- reduce(L,fn (x,y) => if x = 0 then true else y,false);gives true if and only if the list L contains at least a 0.
Consider for instance the functional composition, defined as follows:
The functional composition of f and g is a function that, for every x, gives as result g(f(x))In ML we can define the functional composition in the following way:
fun comp(f,g) = fn x => g(f(x))The type of comp is
comp : ('a -> 'b) * ('b -> 'c) -> ('a -> 'c)where 'a -> 'b represents the type of f, 'b -> 'c represents the type of g, and 'a -> 'c represents the type of comp(f,g).
For instance, comp(fact,fn x => x*2) represents the function which, given n, first computes the factorial of n, and then multiplies it by 2. Thus we have:
- map([1,2,3,4],comp(fact,fn x => x * 2)); val it = [2,4,12,48] : int list
- fun f(x) = x; val f = fn : 'a -> 'aNow, suppose that we want to write the expression
not(f(true));in a language like C or Pascal you could eliminate the external parentheses, and write
not f(true);In ML, however, if we write such a thing, we get
- not f(true); stdIn:117.1-117.12 Error: operator and operand don't agree [tycon mismatch] operator domain: bool operand: 'Z -> 'Z in expression: not fThe reason is that ML tries to interpret the above expression as
(not f)(true)This is because in an expression of the form
op1 op2 constit might be the case that op1 is an higher order function, which takes as an argument a function op2, and gives as result something that can be applied to const. In a language without higher order this interpretation would not make sense, and therefore the rules of priority are usually defined so that op1 op2 const is parsed as op1 (op2 const)
In general, in ML, an expression of the form
e1 e2 e3 ... enis parsed as
(...((e1 e2) e3) ... en)Of course this is just the default. In ML, like in other languages, there are some priority rules (like the precedence of "*" over "+") that may alter this order. In general, if you are unsure of how an expression is parsed, just put explicit parentheses to make sure that it is parsed the way you want.
One simplification that is always allowed in ML, however, is the elimination of the innermost parentheses around a "token". In other words, we can always write f x instead of f(x). Hence, for instance, we can write the identity function as
fun f x = x;and the expression not(f(true)) as
not (f true);
For instance, consider the following function which gives the maximum of a list of integers:
fun maxlist [x] = x | maxlist (x::l) = let val y = maxlist l in if x < y then y else x end;if you compile this definition in SML, you'll get a non-exhaustive matching warning. In fact, the case of emptylist is missing: If you write the expression
maxlist you will get a run-time error.
When the missing cases corresponds to arguments for which the value of the function is undefined, the correct way to eliminate the warnings would be by introducing exceptions (ML allows to handle exceptions in a way similar to C++ and Java).
However, if you are sure that the missing cases do not correspond to interesting cases (i.e. to cases that may present in input) then you can just ignore the warnings of non-ehaustive matching.
For instance, suppose that you want to write a function which construct a balanced tree from a list, and that to this purpose you need to define an internal auxiliary function
half: int * 'a list -> 'a list * 'a listsuch that half(n,l) gives as result the two lists obtained by dividing l in two lists of equal length (plus or minus 1), and suppose that, in your intention, n represents the length of the original list l.
Then you probably will give a definition of the following form:
fun half(0, nil) = ... | half(n,(x::l)) = ... ;this will give you a non-exhaustive matching warning. (since the case (0,non-empty) is missing.) However, if you make sure that in your main program you always call your auxiliary function with an expression of the form
half(n,l);where n is defined as the length of l, then your program will never give a runtime error and you don't need to worry about the warning.
- fun same_length (nil,nil) = true | same_length (x::l,y::k) = same_length(l,k) | same_length(l,k) = false; val same_length = fn : 'a list * 'b list -> bool - - fun same_list(nil,nil) = true | same_list(x::l,y::k) = x=y andalso same_list(l,k) | same_list(l,k) = false; val same_list = fn : ''a list * ''a list -> boolAs we can see, the ML type inference mechanism gives for same_list a type different from the one given for same_length, i.e. we get a parameter ''a instead of 'a. This is due to the fact that, in the definition of same_list, we make an equality test between two elements of the lists (x and y). The double quote notation, in ML, is used to represent a type where equality is defined (equality type).
Examples of equality types are integers, booleans, strings, and any other structure (predefined or used-defined) made by equality types. For instance, pairs of equality types, lists of equality types, trees of equality types etc.
Examples of types on which equality is not defined are functional types and everything constructed with functional types. Thus, if we call the two functions above with lists of functions as arguments, same_length will give an answer true or false while same_list will give a type error.
- same_length([1,2],[2,3]); val it = true : bool - same_list([1,2],[2,3]); val it = false : bool - fun f x = x; val f = fn : 'a -> 'a - fun g x = x; val g = fn : 'a -> 'a - same_length([f],[g]); val it = true : bool - same_list([f],[g]); stdIn:24.1-24.19 Error: operator and operand don't agree [equality type required] operator domain: ''Z list * ''Z list operand: ('Y -> 'Y) list * ('X -> 'X) list in expression: same_list (f :: nil,g :: nil)
datatype 'a btree = emptybt | consbt of 'a * 'a btree * 'a btree;
fun in_traversal emptybt =  | in_traversal (consbt(x,t1,t2)) = (in_traversal t1) @ [x] @ (in_traversal t2);
fun pre_traversal emptybt =  | pre_traversal (consbt(x,t1,t2)) = [x] @ (pre_traversal t1) @ (pre_traversal t2);
fun post_traversal emptybt =  | post_traversal (consbt(x,t1,t2)) = (post_traversal t1) @ (post_traversal t2) @ [x];
- in_traversal (consbt(1,consbt(2,emptybt,emptybt),consbt(3,emptybt,emptybt))); val it = [2,1,3] : int list - pre_traversal(consbt(1,consbt(2,emptybt,emptybt),consbt(3,emptybt,emptybt))); val it = [1,2,3] : int list - post_traversal(consbt(1,consbt(2,emptybt,emptybt),consbt(3,emptybt,emptybt))); val it = [2,3,1] : int list
fun convert_to_0 emptybt = emptybt | convert_to_0 (consbt(x,t1,t2)) = let val u1 = convert_to_0(t1) val u2 = convert_to_0(t2) in if x < 0 then consbt(0,u1,u2) else consbt(x,u1,u2) end;