Held: Wednesday, 28 January 2004
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.
- As a change of pace, today's class will primarily focus on group problem solving.
- A problem: Turning numbers into letters (in English and Scheme).
- Making life easier: Conditionals.
- What kinds of tests can you use in conditionals?
- 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.
- 94-100: A, 90-93: A-, 87-89: B+, 84-86: B, ...
- 10:A+ 9:A, 8:A-, 7:B+, 6:B, 5:B-, 4:C+ 3:C 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
Marie's evaluation 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 (>= grade 94)
(if (<= 90 grade 93)
(if (<= 87 grade 90)
- 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.
((>= grade 94) 'A)
((<= 90 grade 93) 'Aminus)
- 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? typically 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) and compares the components of those types.
- Scheme also lets you combine Boolean expressions with
- You will have an opportunity to experiment with all of these
in the lab.