Held: Friday, 12 September 2003
Today we begin to learn how to tell Scheme to make decisions. The
procedures used to make decisions are called conditionals.
Today's class will be a large group design effort rather than an individual
or small group design effort.
- No reading this weekend. Please work on the lab writeup.
- Info on the lab writeup now posted. Look at it. It has many useful links.
- Did anyone go to the Wednesday talks? If so, please email me.
- A problem: Turning numbers into letters (in English and Scheme).
- Making life easier: Conditionals.
- Conditionals in Scheme.
- Problem, Revisited.
- Suppose that for fairness's sake, I'd like a program that converts
numeric grades to letter grades.
- We'll look at two related grading schemes.
- 90-100: A, 80-89: B, 70-79: C, 60-69: D, 0-59: F.
- 4: A, 3: B, 2: C, 1: D, 0: F.
- How might you explain to someone how to turn a number into a grade?
- In either schema, can you write a procedure that turns a number
into a grade, using only the Scheme you already know?
- As you may have noted from your narrative, we are often tempted
to describe the procedure in terms of the word
if the grade is at least 94, then give the student
an A, otherwise, if the grade is between 90 and 93, ...
- In Scheme, you can write such expressions as
- Note that
if does not behave according to the standard
pattern, in which all the arguments
to a procedure are computed before the procedure is applied).
- In particular, only one of the success-expression and
failure-expression is computed.
- Given the Scheme that you know right now, the only effect is
- Later, you'll see that this decision affects other things, too.
- For example
- We can nest these conditions.
(if (<= 90 garde)
(if (<= 80 grade 89)
(if (<= 70 grade 79)
- We can take advantage of knowledge that previous tests failed.
(if (<= grade 90)
(if (<= 80 grade)
(if (<= 70 grade)
- As you may have noticed, the code is somewhat hard to read (and
awfully indented). Another alternative is to use Scheme's
cond, which more closely resembles a table.
- Here's what a
cond expression looks like
- In this case, the conditions are evaluted one-by-one until one
holds true. The value of the corresponding expression
is the then returned.
- For our problem, this format seems particularly appropriate.
((<= 90 grade) 'A)
((<= 80 grade) 'B)
- Can you write code to convert numeric to letter that uses neither
- Hint: You can do it with just the ideas you learned before today.
- To permit you to write useful tests and conditions for conditionals,
Scheme provides a wide variety of mechanisms for building those tests.
- Tests should return true (
#t) or false
- Scheme treats anything that's not false as true.
- We call the tests Boolean expressions in honor of the logician
- We call procedures that return true or false predicates.
- The simplest tests: Type Predicates.
- These procedures (whose names invariably end with question marks)
determine whether a value falls within a particular type.
- They include:
- There are many numeric comparators, including
< (strictly less than),
<= (less than or equal to),
= (equal to),
>= (greater than or equal to),
> (strictly greater than).
- There are also a variety of equality comparators, including
= is used for numbers.
eq? determines if two
values share the same memory location.
eqv? determines if two
primitive values are equal.
equal? also looks within
compound types (such as lists).
- Scheme also lets you combine Boolean expressions with
- You will have an opportunity to experiment with all of these
in tomorrow's lab.
Thursday, 28 August 2003 [Samuel A. Rebelsky]