CS 227 lecture -*- Outline -*- * conditionals so far we can combine calls to procedures but they always do the same thing to their arguments want them to do different things depending on their arguments ** example translate some words into German want function german such that ---------------- TRANSLATION BY CASES (german 'one) = 'eins (german 'day) = 'tag (german 'good) = 'gut (german x) = 'ich-weiss-es-nicht ; TYPE: (-> (symbol) symbol) ---------------- problem is to tell the blind robot how to do it... the robot can't see the word, but can ask questions what Scheme questions? need a way to tell the robot how to combine the answers ** cond ---------- (define german ; portable (lambda (word) (cond ((eq? word 'one) 'eins) ((eq? word 'day) 'tag) ((eq? word 'good) 'gut) (else 'ich-weiss-es-nicht)))) (DEFINE german ; not portable (LAMBDA (word) (COND [(eq? word 'one) 'eins] [(eq? word 'day) 'tag] [(eq? word 'good) 'gut] [ELSE 'ich-weiss-es-nicht]))) ---------- either form is okay, first more portable *** syntax explain general case point out keywords, clauses ------------ THE COND SPECIAL FORM (COND (expr11 expr12) (expr21 expr22) ... (ELSE exprn2)) ------------ explain what is meant by "special form" give examples with #t and #f in various test positions. Can they summarize what it does? *** use work out the following using cond. ----------------- (pair-w-car-number? (cons 1 '())) ==> #t (pair-w-car-number? 'hmmm) ==> #f (pair-w-car-number? (cons 'h '())) ==> #f What type? pair-w-car-number? : ------------------ Have the students do the following in groups 30 sec for the type, raise your hand as soon as you are done. ---------------- (symbol-or-number? 227) ==> #t (symbol-or-number? 'isu) ==> #t (symbol-or-number? #t) ==> #f (symbol-or-number? (cons 1 2)) ==> #f symbol-or-number? : ----------------- ** if ---------- THE IF SPECIAL FORM (DEFINE german (LAMBDA (word) (IF (eq? word 'one) 'eins (IF (eq? word 'day) 'tag (IF (eq? word 'good) 'gut 'ich-weiss-es-nicht))))) SYNTAX (if test expr1 expr2) ------------ makes a 2-way choice example: (if (number? menu) (+ 3 4) (+ 200 27)) translate pair-w-car-number? Have them form groups to do symbol-or-number? with if *** COND and IF are EXPRESSIONS ---------- COND and IF are EXPRESSIONS (DEFINE german-greeting-redundant (LAMBDA (thyme) (IF (eq? thyme 'morning) (cons 'guten (cons 'morgen '())) (cons 'guten (cons 'tag '()))))) (DEFINE german-greeting (LAMBDA (thyme) (cons 'guten (cons (IF (eq? thyme 'morning) 'morgen 'tag) '())))) (DEFINE better-greeting (LAMBDA (thyme) ; REQUIRES: word is one of morning, ; afternoon, or evening (cons 'guten (cons (COND [(eq? thyme 'morning) 'morgen] [(eq? thyme 'afternoon) 'tag] [(eq? thyme 'evening) 'abend]) '())))) ---------------- (the word "time" is reserved in Chez Scheme, hence thyme... :-) do the above on the computer (load "german.ss") ** summary of if and cond ---------------- EQUATIONS FOR IF, COND EXPRESSIONS (if test (c x) (c y)) = (c (if test x y)) (COND [expr1 (c x)] [expr2 (c y)] [ELSE (c z)]) = (c (COND [expr1 x] [expr2 y] [ELSE z]) ---------- ------------ SUMMARY OF IF AND COND (IF #t expr1 expr2) = expr1 (IF #f expr1 expr2) = expr2 (IF (not test) e1 e2) = (IF test e2 e1) (COND [test expr1] [ELSE expr2]) = (IF test expr1 expr2) (COND [expr11 expr12] [expr21 expr22] [ELSE exprn2]) = (IF expr1 expr12 (IF expr21 expr22 exprn2)) ---------- Use these equations to convert from if to cond and back, e.g. to simplify nested if's ** and, or, not play with these and ask them to summarize note that only "not" is a proc, others are keywords because AND and OR are defined using IF, they are short curcuit (define bar '()) (eq? (car 'bar) 'x) ; error (and (pair? bar) (eq? (car bar) 'x)) ; ok show how to do pair-w-car-number? have them do symbol-or-number? in groups ---------- AND, OR, and NOT SUMMARY (not #t) = #f (not #f) = #t for all expr1:boolean, expr2:boolean, (OR expr1 expr2) = (IF expr1 #t expr2) (AND expr1 expr2) = (IF expr1 expr2 #f) (not (OR e1 e2)) = (not (IF e1 #t e2)) = (IF e1 (not #t) (not e2)) = (IF e1 #f (not e2)) = (IF (not e1) (not e2) #f) = (AND (not e1) (not e2)) ---------- can use the equality of IF and COND to rewrite CONDs... --------------- MULTIPLE ARGUMENTS (OR e1 e2 e3 ...) = (OR e1 (OR e2 e3 ...)) (AND e1 e2 e3 ...) = (AND e1 (AND e2 e3 ...)) -------------- AND and OR can take 0 or more args (try this) *** review logic with #t and #f esp point out (or #t #t) is not exclusive, can be both *** more examples (if time) have them do the second one if their solutions use if or cond, translate to or... --------------- num-then-symbol? : (-> (datum) boolean) (num-then-symbol? '(703 isu)) ==> #t (num-then-symbol? '(isu 703)) ==> #f (num-then-symbol? 'isu) ==> #f (num-then-symbol? 703) ==> #f --------------- first number, then symbol, see p. 45 --------------- singleton-list? : (-> (datum) boolean) (singleton-list? '(703)) ==> #t (singleton-list? '(703 704)) ==> #f (singleton-list? 703) ==> #f --------------- one elem list how to tell? cdr is empty ---------- ;; program 2.1 (define singleton-list? (lambda (ls) (and (pair? ls) (null? (cdr ls))))) ---------- *** summary of elimination Use the following to summarize elimination rules -------------- ELIMINATING #t and #f (COND [expr1 #t] [expr2 #t] [ELSE exp]) = (IF expr11 #t (IF expr21 #t exp)) = (OR expr11 (OR expr21 exp)) = (OR expr11 expr21 exp) (COND [(not expr1) #f] [(not expr2) #f] [ELSE exp]) = (IF (not expr11) #f (IF (not expr21) #f exp)) = (IF expr11 (IF expr21 exp #f) #f) = (AND expr11 (AND expr21 exp)) = (AND expr11 expr21 exp) --------------