CSC151 2007S, Class 10: Conditionals
Admin:
* Due: HW 5
* Homework 6 is now available.
* EC: Thursday's 4:30 talk on An Online Campus Map.
* EC: Tommy swims this weekend (all day Friday to Sunday)
* The next reading is on recursion. Note that the reading is
complex enough that we'll spend two days on it.
* Read -> Lab+Discussion -> Reread -> Continue lab
Overview:
* Preliminary Notes on Conditionals.
* An example: Converting between numeric grades and letter grades
* Lab.
* Reflections.
/Lessons from the Reading/
* You can write if "statements"
(if TEST CONSEQUENT ALTERNATE)
* Meaninga
* Evaluate the test
* If the value of test is #f, evaluate the alternate and return
its value
* If the value of the test is not #f, evaluate the conseqeunt
and return its value
* (if (< a b) a b) - Gives you the smaller of a and b
* (if (>= grade 60) 'pass 'fail) - If grade is >= 60, gives the
symbol pass, o/w, gives the symbol 'fai
Problem: Convert letter grade (A, B, C, D, F) to a number grade
(define letter->number
(lambda (grade)
(if (equal? grade 'A) 96
(if (equal? grade 'B) 86
(if (equal? grade 'C) 76
(if (equal? grade 'D) 66
50))))))
(define number->letter
(lambda (grade)
(if (<= 90 grade 100) 'A
An alternate, teh cond
(cond
(TEST1 EXP1)
(TEST2 EXP2)
...
(TESTn EXPn)
(else ALTERNATE))
* Meaning:
Evaluate TEST1. If it holds (not false), use EXP1
Otherwise, evaluate TEST2. ...
---
(define letter->number
(lambda (grade)
(if (equal? grade 'A) 96
(if (equal? grade 'B) 86
(if (equal? grade 'C) 76
(if (equal? grade 'D) 66
50))))))
(define number->letter
(lambda (grade)
(if (<= 90 grade) 'A
(if (<= 80 grade) 'B
(if (<= 70 grade) 'C
'D)))))
(define number->letter
(lambda (grade)
(cond
((<= 90 grade) 'A)
((<= 80 grade) 'B)
((<= 70 grade) 'C)
(else 'D))))
(define valid-date?
(lambda (month day)
(or (and (= month 2) (<= 0 day 28))
(and (or (= month 9) (= month 4) (= month 6) (= month 11))
(<= 0 day 30))