CSC153 2004S, Class 5 or so: Conditionals Admin: * Homework due. Graded "real soon now". * New homework to be assigned tomorrow. * Reading for tomorrow: Recursion * Don't forget Convo! * Please try to arrive on time (Sam, Andrew, Ilan) Overview: * A problem (to solve in English) * Simple conditionals for solving the problem * Advanced conditionals * Alternate solution * Lab Problem: * Sam has lots and lots of grades in numeric form (92.3, 83.7, ...) * The registrar expects grades in letter form * Sam wants a program to express what needs to be done "Background check ... what should we do for boundary cases ..." "Compare the number to the ranges" -> "Incompatible type error. 93..100 is not a number." if the grade is at least 93, give the student an A if the grade is at least 90, give the student an A- if the grade is at least 87, give the student a B+ ... * Potential problem: Might give too many grades * Solution: Tell the function to "stop" * Alternate solution: Use "else"/"otherwise" if the grade is at least 93, give the student an A otherwise, if the grade is at least 90, give the student an A- otherwise, if the grade is at least 87, give the student a B+ ... * Alternate solution: Use two conditions if the grade is at least 93, give the student an A if the grade is at least 90 and less than 93, give the student an A- if the grade is at least 87 and less than 90, give the student a B+ ... (if TEST TRUE-RESULT FALSE-RESULT) Model: Evaluates the test. If the test is false, evaluates the false result and returns its value. If the test is anything but false, evaluates the true result and returns its value. (if (>= grade 93) 'A (if (>= grade 90) 'A- (if (>= grade 87) 'B+ F))) Designers of Scheme came up with alternative syntax for series of conditionals (cond (TEST EXP) (TEST EXP) (TEST EXP) (else EXP)) idea: Conduct each test in sequence. When one holds, use the value of the corresponding expression. Challenge question: Can you assign symbolic grades to numeric grades WITHOUT using a conditional? * Round the numbers and use corresponding ASCII values. * Build a lookup table and index into the list (list-ref (round grade) (list 'F 'D 'C 'B 'A)) Try the lab! NOTES: * The null? predicate tests for an empty list * Many of you seem to have found the "eq?" vs "eqv?" vs "equal?" problem puzzling * = vs. eqv * Naming conventions: The names of predicates (true-false procedures) end with ? * Style in writing predicates: Avoid (if TEST #t ...) and the ilk (define in-range? (lambda (val) (if (>= val 0) (if (<= val 100) #t #f) #f))) (define in-range? (lambda (val) (and (>= val 0) (<= val 100)))) (define in-range? (lambda (val) (<= 0 val 100))) See you tomorrow!