High Order Functions

LA home
Computing
 Algorithms
 Bioinformatics
 FP,  λ
 Logic,  π
 MML
 Prog.Langs

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.
window on the wide world:

Computer Science Education Week

Linux
 Ubuntu
free op. sys.
OpenOffice
free office suite,
ver 3.4+

The GIMP
~ free photoshop
Firefox
web browser
FlashBlock
like it says!

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

© L. Allison   http://www.allisons.org/ll/   (or as otherwise indicated),
Faculty of Information Technology (Clayton), Monash University, Australia 3800 (6/'05 was School of Computer Science and Software Engineering, Fac. Info. Tech., Monash University,
was Department of Computer Science, Fac. Comp. & Info. Tech., '89 was Department of Computer Science, Fac. Sci., '68-'71 was Department of Information Science, Fac. Sci.)
Created with "vi (Linux + Solaris)",  charset=iso-8859-1,  fetched Saturday, 27-Dec-2014 10:33:56 EST.