CS 227 lecture -*- Outline -*- * case expressions ------------------------ (vowel? 'a) ==> yes (vowel? 'c) ==> no (vowel? 'y) ==> sometimes (define vowel? ; TYPE: (-> (symbol) ans) (lambda (letter) (cond ((or (eqv? letter 'a) (eqv? letter 'e) (eqv? letter 'i) (eqv? letter 'o) (eqv? letter 'u)) 'yes) ((or (eqv? letter 'y) (eqv? letter 'w)) 'sometimes) (else 'no)))) ------------------------- ** syntax ------------------------- THE CASE EXPRESSION syntax: (CASE expr ((key11 key12 ...) body1) ... (ELSE body-n+1)) ----------------------- the keys are the external representations of Scheme objects they do NOT use quotes ----------------------- example: (define vowel? ; TYPE: (-> (symbol) ans) (lambda (letter) (case letter ((a e i o u) 'yes) ((y w) 'sometimes) (else 'no)))) ---------------------- semantics: evaulate the expression expr, call it's value the "target" compare (using eqv?) to each key. return value of first key that matches the target do else otherwise. (case 3 ((1 3) 'foo) ((3) 'bar) (else 'baz)) ==> foo