lec5 - CSE 341 Spring 2008 Lecture 5...

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

View Full Document Right Arrow Icon
(* CSE 341, Spring 2008 *) (* Lecture 5: pattern-matching (including lists, options, tuples, and records) functions take 1 argument tail recursion accumulators *) (* defines trees where leaves are constants holding integers and internal nodes are negations with one child or additions with two children *) datatype arith_exp = Constant of int | Negate of arith_exp | Add of arith_exp * arith_exp val test_exp = Add (Constant 19, Negate (Constant 4)) v fun eval e = case e of Constant i => i | Negate e2 => ~ (eval e2) | Add(e1,e2) => (eval e1) + (eval e2) val fifteen = eval test_exp v fun max_constant e = case e of Constant i => i | Negate e2 => max_constant e2 | Add(e1,e2) => let val m1 = max_constant e1 val m2 = max_constant e2 in if m1 > m2 then m1 else m2 end datatype my_int_list = Empty | Cons of int * my_int_list val one_two_three = Cons(1,Cons(2,Cons(3,Empty))) v fun append_mylist (l1,l2) = (* a local function could avoid passing l2 *) case l1 of Empty => l2 | Cons(hd,tl) => Cons(hd, append_mylist(tl,l2))
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the 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 / 3

lec5 - CSE 341 Spring 2008 Lecture 5...

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