lec26 - (* simple expressions *) datatype exp = Int of int...

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

View Full Document Right Arrow Icon
(* simple expressions *) datatype exp = Int of int | Negate of exp | Add of exp * exp | Mult of exp * exp fun eval e = case e of Int i => i | Negate e1 => 0 - (eval e1) | Add(e1,e2) => (eval e1) + (eval e2) | Mult(e1,e2) => (eval e1) * (eval e2) fun makeString e = case e of Int i => Int.toString i | Negate e1 => "-(" ^ (makeString e1) ^ ")" | Add(e1,e2) => "(" ^ (makeString e1) ^ " + " ^ (makeString e2) ^ ")" | Mult(e1,e2) => "(" ^ (makeString e1) ^ " * " ^ (makeString e2) ^ ")" fun hasZero e = case e of Int i => i=0 | Negate e1 => hasZero e1 | Add(e1,e2) => (hasZero e1) orelse (hasZero e2) | Mult(e1,e2) => (hasZero e1) orelse (hasZero e2) (* now we want a new operation -- easy *) fun sumInts e = case e of Int i => i | Negate e1 => sumInts e1 | Add(e1,e2) => (sumInts e1) + (sumInts e2) | Mult(e1,e2) => (sumInts e1) + (sumInts e2) (* now we want a new variant -- not easy *) datatype myexp_wrong = OldExp of exp | Power of myexp_wrong * myexp_wrong fun pow (x:int, y:int) = (* assumes y >= 0 *)
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 / 2

lec26 - (* simple expressions *) datatype exp = Int of int...

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