High Order Functions

LA home
Computing
FP
 SML
  SML97
   High O.Fn

Also see:
   Conts.
  Curried

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

  1. 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
  2. Rewrite foldl and foldr so that parameter f is curried, and rewrite sum and product to match.
www #ad:

SML:
:: cons
[x1,...] list
[ ] list
@ append
fn =>  &lambda .
: has type
Compared

↑ © L. Allison, www.allisons.org/ll/   (or as otherwise indicated).
Created with "vi (Linux)",  charset=iso-8859-1,   fetched Friday, 26-Apr-2024 20:17:51 UTC.

Free: Linux, Ubuntu operating-sys, OpenOffice office-suite, The GIMP ~photoshop, Firefox web-browser, FlashBlock flash on/off.