lec7 - case lst of => | fst::rest =>...

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

View Full Document Right Arrow Icon
(* Dan Grossman, CSE341 Spring 2008, Lecture 7 *) ( fun n_times (f,n,x) = (* what "happens" if we give x explicit type int *) if n=0 then x else f (n_times(f,n-1,x)) fun double x = x+x f fun increment x = x+1 f val x1 = n_times(double,4,7) val x2 = n_times(increment,4,7) val x3 = n_times(tl,2,[4,8,12,16]) (* bad type w/ hd *) v fun n_doubles (n,x) = n_times(double,n,x) f fun n_triples1 (n,x) = n_times((let fun f y = 3*y in f end), n, x) fun n_triples2 (n,x) = n_times((fn y => 3*y), n, x) f fun map (f,lst) = case lst of [] => [] | fst::rest => (f fst)::(map(f,rest)) val x4 = map (increment, [4,8,12,16]) v val x5 = map (hd, [[1,2],[3,4],[5,6,7]]) v fun twice_on_increment (f,x) = (* takes a function, but not polymorphic *) f ((f (x+1)) + 1) fun filter (f,lst) =
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: case lst of => | fst::rest => if f fst then fst::(filter (f,rest)) else filter (f,rest) fun is_even v = (v mod 2 = 0) fun get_all_even lst = filter(is_even,lst) fun get_all_even_snd lst = filter((fn (_,v) => is_even v), lst) val get_all_even_snd2 = fn lst => filter((fn (s,v) => v mod 2 = 0), lst) fun double_or_triple f = if f 7 then fn x => 2*x else fn x => 3*x datatype exp = Constant of int | Negate of exp | Add of exp * exp fun true_of_all_constants(f,e) = case e of Constant i => f i | Negate e1 => true_of_all_constants(f,e1) | Add(e1,e2) => true_of_all_constants(f,e1) andalso true_of_all_constants(f,e2) fun all_even e = true_of_all_constants(is_even,e)...
View Full Document

Page1 / 2

lec7 - case lst of => | fst::rest =>...

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