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

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

(* 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 *)

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

View Full Document
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
Ask a homework question - tutors are online