(* lecture 8: curried functions; the "real" versions of map/filter/reduce *) use "utility.sml"; (* so that I can use foldl, foldr, filter *) open List; (* len function computes the length of the given list. This version uses curried functions. *) fun len(lst) = foldl op+ 0 (map (fn(x)=>1) lst); val lentest = [10, 20, 30, 40]; (* curried version of pow function. its type is (int -> int -> int) *) fun pow x 0 = 1 | pow x y = x * pow x (y-1); (* A currying example. sum3 takes 3 parameters and adds them. *) fun sum3 a b c = a+b+c; (* f is a partial instantiation of sum3. f is a new function that binds sum3's "a" parameter to be 42, and accepts the other two parameters b and c. *) fun f = sum3 42; (* g binds sum3's a and b to be 42 and 9, and accepts the third parameter c. *) fun g = f 9; val result = g 24; (* result = 42+9+24 = 75 *) (* Examples of function composition, and the 'real' curried version of map. *) val x = map (round o Math.sqrt o real) (1--100); fun squareWhole lst = map (real o (fn x => x*x) o trunc) lst; val sqtest = [3.4, 1.7, 5.8, 10.6];