lec8 - fun map (f,l) = case l of => | fst::rest => (f

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
(* CSE341, Spring 2008, Lecture 8 *) ( (* Create similar functions *) ( val addn = fn n => fn m => n+m v val increment = addn 1 v val add_two = addn 2 v fun f n = if n=0 then [] else (addn n)::(f (n-1)) (* Combine functions *) ( fun f1 (g,h) = fn x => g (h x) f (* f1 is actually in ML's standard library as infix o (composition) *) (* but that is just syntax -- the result of g o h is a closure that "remembers" g and h in its environment *) fun sqrt_of_abs1 i = Math.sqrt(Real.fromInt (abs i)) f fun sqrt_of_abs2 i = (Math.sqrt o Real.fromInt o abs) i f val sqrt_of_abs3 = Math.sqrt o Real.fromInt o abs v (* another combining-fuctions function *) fun f2 (g,h) = fn x => case g x of NONE => h x | SOME y => y (* Private data, for map/fold *) ( (* Earlier we saw map: *) (
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: fun map (f,l) = case l of => | fst::rest => (f fst)::(map(f,rest)) (* This fold function is at least as useful *) fun fold (f,acc,l) = case l of => acc | fst::rest => fold (f, f(acc,fst), rest) (* fold is actually in ML's standard library as List.foldl, but List.foldl is "curried" (explanation soon). You will use List.foldl in homework 3 *) (* Example uses that do not use private data *) ( fun f3 l = fold ((fn (x,y) => x+y), 0, l) f fun f4 l = fold ((fn (x,y) => x andalso y >= 0), true, l) f (* Examples that use that uses private data *) ( fun f5 (l,lo,hi) = fold ((fn (x,y) => if y >= lo andalso y <= hi then x+1 else x), 0, l) fun f6 (lst,hi) = map((fn x => if x > hi then hi else x), lst)...
View Full Document

This note was uploaded on 10/12/2009 for the course CSE 341 taught by Professor Staff during the Spring '08 term at University of Washington.

Page1 / 2

lec8 - fun map (f,l) = case l of => | fst::rest => (f

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online