(* A set of utility functions to be used as helpers by your other programs. To use them, write the following at the top of your file: use "utility.sml"; *) (* Applies function F to each element of the given list [a,b,c,...], producing a new list [F(a),F(b),F(c),...]. *) fun map341(F, []) = [] | map341(F, first::rest) = F(first) :: map341(F, rest); (* Calls function P on each element of the given list [a,b,c,...]; P is an inclusion test that returns either true or false. The elements for which P returns true are kept; the others discarded. *) fun filter(P, []) = [] | filter(P, first::rest) = if P(first) then first :: filter(P, rest) else filter(P, rest); exception EmptyList; (* Calls function F successively on pairs of elements from the given list to combine pairs into a single element until only one element remains. *) fun reduce(F, []) = raise EmptyList | reduce(F, [value]) = value | reduce(F, first::rest) = F(first, reduce(F, rest)); (* A helper operator to produce a range of numbers; a--b produces [a, a+1, ..., b-1, b]. *) infix --; fun min--max = if min > max then [] else min :: (min + 1 -- max); (* Converts an infix operator into curried form. *) fun curry operator a b = operator(a, b);