High Order Functions
The following high-order functions are well-known in functional programming and most FP libraries provide most of them, or equivalents.
fun curry f x y = f(x,y) and uncurry f(x,y) = f x y and map f [] = [] | map f (x::xs) = (f x)::(map f xs) and foldl f z [] = z | foldl f z (x::xs) = foldl f (f(z,x)) xs (* BTW the opr, f, is curried in the Haskell version *) and foldr f z [] = z | foldr f z (x::xs) = f(z, (foldr f z xs)) val sum = foldl (op +) 0 and product = foldl (op * ) 1; (* ------------------------------------------tests-- *) val l=[1,2,3,4]; sum l; product l; (* well known high order fns. LA, CSSE, 22/6/2005 *) |
Exercises
- Which of the following are legal, and if so what is the type,
or illegal, and if so why?
- curry curry
- curry uncurry
- uncurry curry
- uncurry uncurry
- Rewrite foldl and foldr so that parameter f is curried, and rewrite sum and product to match.