CSC151.01 2006S, Class 10: Conditionals Admin: * We'll begin class with a minute of silence for the losses of five years ago. * And I'll try not to make political statements. * Returned (implicitly): HW4. * If you didn't hear from me, you got a check. * If you heard "I didn't receive it", please talk to me. * If you heard anything else, you got that grade. * No, I won't be grading HW3. * Don't forget tomorrow's Tuesday extra on "Finding Significance in Insignificance" (1:00-2:05, Science 2424). * Don't forget that we're in 2435 tomorrow. * The computers were down, so (a) there was no lab and (b) this EBoard is simulated. Overview: * Notes on HW4. * Preliminary Notes on Conditionals. * Lab. * Reflections. /Introduction to Conditionals/ * Conditionals are control structures that allow you to make choices. * Example 1 (from Sam's teaching algortihm): "If the computers are working, make the students do today's lab. Otherwise, blather aimlessly for 45 minutes." * Example 2 (also from Sam's teaching algorithm): "If a student shows up five minutes late, have everyone stand up and say 'Hi Vince!'" * A bug in the algorithm: Right now, students say "Hi Vince" no matter who whows up five minutes late. /Simple Conditionals/ * The simplest conditionals in Scheme are given by the "if" structure. (if TEST CONSEQUENT ALTERNATE) * Evaluation strategy: * Evaluate the test. * If the test is false ("does not hold"), * Evaluate the alternate * Return its value * If the test is not false ("holds") * Evaluate the consequent * Return its value * Example one: max * Find the largest of two values (define max (lambda (a b) (if (> a b) a b))) * Some issues: * If a and b are equal, returns b. That's okay. * We originally had "(display a)", but we want to return the value (perhaps for use by another procedure), not just show it. * Sam likes to hear you pronounce paranes. * Example two: grading * Sam's overly complex grading Scheme: if you turn something in if it's beautiful award it a Plus otherwise, if it works award it a Check otherwise, award it a Minus if you don't turn something in award it a Zero * In Scheme (define grade (lambda (hw) (if (turned-in? hw) (if (beautiful? hw) 'plus (if (works? hw) 'check 'minus)) 'zero))) * Notes: * Identation helps with understanding. * Yes, we'll need to write turned-in?, beautiful?, and works? * Nested ifs can become confusing. * The grading strategy is strange. /Another Condtional: cond/ * Format (cond (TEST CONSEQUENT) (TEST CONSEQUENT) ... (TEST CONSEQUENT) (else ALTERNATE)) * Meaning * Evalute the first TEST. * If it holds (is not false), * evaluate the CONSEQUENT and return its value. * If the first test fails, evaluate the second test, returning the value of its CONSEQUENT if it holds.. * And so on and so forth. * If no test holds, return the value of the ALTERNATE. * Notes: * The *first* test that succeeds is used, even if multiple tests succed. * Everything after that test is, in effect, discarded. * Tests are typically parenthesized. * An example, grading, revisited (define grade (lambda (hw) (cond ((and (turned-in? hw) (works? hw) (beautiful? hw)) 'plus) ((and (turned-in? hw) (or (works? hw) (beautiful? hw))) 'check) ((turned-in? hw) 'minus) (else 'zeo)))) * Note (again) that the "(turned-in? hw)" is acceptable, since it is only reached if the previous steps failed. * And another grading example (too many of these): Given a numeric grade, returnt he corresponding letter grade. (define letter-grade (lambda (numeric-grade) (cond ((>= numeric-grade 100) "A+") ((>= numeric-grade 94) "A") ((>= numeric-grade 90) "A-") ... (else "C+")))) * Notes * We don't need to write (>= 100 numeric-grade 94) for the "A" case, because we know the failure of the first test suggests that numeric-grade < 100. * There were at least three ways to write the test for the "A" part * (>= numeric-grade 94) -- what we chose. * (>= 100 numeric-grade 94) -- gives additional immediate info to the reader. But "wordy". * (<= 94 numeric-grade 100) -- similar. * (<= 94 numeric-grade) -- we chose the >= to parallel the first clause. * The last example: "typing" values ;;; Procedure: ;;; type-of ;;; Parameters: ;; val, a Scheme value ;;; Purpose: ;;; Determine the 'type' of val (number, string, symbol, etc.) ;;; Produces: ;;; type, a symbol ;;; Preconditions: ;;; (None) ;;; Postconditions: ;;; (type? val) returns #t (where type? is number? when type is ;;; 'number, string? when type is 'string, and so on and ;;; so forth. ;;;' Practica: ;;; > (type-of 'e) ;;; symbol ;;; > (type-of e) ;;; number ;;; > (type-of "e") ;;; string * And our code (define type-of (lambda (val) (cond ((boolean? val) 'boolean) ((character? val) 'character) ((string? numbe) 'number) ((procedure? val) 'procedure) ((string? val) 'string) ((symbol? val) 'symbol) (else 'somtehing-sa-forgot-to-teach)) Class ended with a moment of silence for the events of five years ago.