(* higher order functions in ML *) fun mymap(f,[]) = [] | mymap(f,(h::t)) = ((f h)::mymap(f,t)); fun filter(f,[]) = [] | filter(f,(h::t)) = if (f h) then (h::filter(f,t)) else filter(f,t); (* filter( (fn x => (x < 5)), [5, 2, 7, 4, 8] ) -> [2,4] *) fun fold(f,ident,[]) = ident | fold(f,ident,(h::t)) = f(h, (fold(f,ident,t))); fun evens(L) = filter (fn x => x mod 2 = 0) L; fun myand(x,y) = x andalso y; fun g5(x) = x>5; fun ag5(L) = fold(myand,true,mymap(g5,L)); (* fun f1(x,y) = x+y; fold(f1,0,[2,4,6,8]) => 20 cell fold(int (*f)(int,int),int ident, cell L) { if (L == NULL) return L; else return (*f)(L->head,fold(f,ident,L->tail)); } *)