CS151, Class 10: Conditionals
Overview:
* A problem: Converting numeric grades to letters.
* Using Scheme's "if".
* Using Scheme's "cond".
* Doing without conditionals
* Spare time for more work on yesterday's lab.
Notes:
* Laboratory writeup 1 assigned (due Friday)
+ Be sure to read the guidelines
+ Be sure to read my criticisms from last year
(for a slightly different assignment)
* Today will be mostly lecture and recitation.
----------------------------------------
Turning numbers into letters
90-100 A
80-89 B
70-79 C
60-69 D
0-59 F
if the numeric grade you need to convert is greater than
90 and less than 100, give the person an A.
Problems:
* What if someone has a 105?
* "greater than or equal to" rather than "greater than"
if the numeric grade you need to convert is greater than
or equal to 90 and less than or equal to 100, give the
person an A.
Suggestion:
* Use "greater than 89" rather than "greater than or equal
to 90"
if the numeric grade you need to convert is greater than
or equal to 80 and less than or equal to 89, give the
person a B.
Concern:
* What happens with people whose grades are greater than
89 and less than 90?
Solutions:
* Change the chart
* Require grader to round up before grading.
if the numeric grade you need to convert is greater than
or equal to 70 and less than or equal to 79, give the
person a C.
if the numeric grade you need to convert is greater than
or equal to 60 and less than or equal to 69, give the
person a D.
if you haven't given the person a grade yet, fail him or her.
----------------------------------------
To do this in Scheme, we need a way to write all of these
"ifs".
(if ____ ___________ ____________)
TEST TRUE-RESULT FALSE-RESULT
Meaning:
* Evaluate TEST.
* If its value is false, evaluate FALSE-RESULT and use it.
* If its value is not false, evaluate TRUE_RESULT and use it.
Sam's simplest grading scheme
(if (>= grade 90) 'A 'F)
"If the grade is at least 90, including 90, the student has
earned an A. Otherwise, the student has earned an F."
(if (>= grade 90)
'A
(if (>= grade 80)
'B
'C))
Because this gets hard to read, Scheme provides an alternative
(cond
(test1 exp1)
(test2 exp2)
(test3 exp3)
...
(testn expn)
(else default))
Meaning
* Evaluate test1. If it evaluates to something other than
false, use the value of exp1.
* Otherwise, go on to test2. If it evaluates to something
other than false, use the value of exp2.
* And so on and so forth.
* If none of the tests hold, use the value of default.
Don't worry about the "not false"; it's usually "true".
(See, computer scientists can be philophers who worry about
truth and falsity).
(cond
((<= 90 grade) 'A)
((<= 80 grade 89) 'B)
((<= 70 grade 79) 'C)
((<= 60 grade 69) 'D)
(else 'F))
Observation: Too much testing!
(cond
((<= 90 grade) 'A)
((<= 80 grade) 'B)
((<= 70 grade) 'C)
((<= 60 grade) 'D)
(else 'F))
Take five minutes and make sure I haven't been lying to you.
Try entering one of these things into DrScheme (preferably
as a procedure).
Can you solve the following similar grading problem without
using if or cond (using only the Scheme you learned before
today).
A: 4
B: 3
C: 2
D: 1
F: 0
(still going from numeric to letter)
I'll ask you at 2:50
Alex's idea: Use list-ref which grabs something from
a particular place in a list.
(list-ref lst place)
(define grades (list 'F 'D 'C 'B 'A))
(define number->letter
(lambda (grade)
(list-ref grades grade)))
Final challenge: Can you use a similar technique to do the
original grading scheme (without using a list of length 100)?
See you tomorrow!