Held Wednesday, September 11, 2002
Today we continue our study of conditionals by working with them
- Coffee and Poptarts.
- On Friday, we're going to have our first
pause for breath
of the semester. We'll review what we've done to date and look
at some of the key problems in more depth.
- On Friday, I'll also assign the first lab writeup and discuss the
procedures for writing and turning in lab reports. It is likely
that I will ask you to write up some problems from the
procedures lab and
some problems from the conditionals
- Almost everyone turned in HW1 long before class.
- The times some of you sent your homework were depressing.
- "Site" and "Cite" are different words.
- As we saw in our PBJ algorithm, many algorithms have to make choices
based on the input.
- We might do different things if we have a new jar or old.
- We might do different things if our twisty-tie is twisted clockwise
- Scheme provides a number of tools for dealing with these kinds
- The simplest is
- The normal form for
(if test true-exp false-exp)
- The meaning is
- Evaluate test.
- If test is false, evaluate false-exp and use its value.
- If test is not false, evaluate true-exp and use its
- There are times in which we want to decide between many different
alternatives. For example, in assigning a letter grade, we give
an A for 94-100, an A- for 90-93, a B+ for 87-89, and so on and
- We can write that with a lot of nested
(if (<= 94 numgrade 100) 'A
(if (<= 90 numgrade 93) 'Aminus
(if (<= 87 numgrade 89) 'Bplus
- However, it is easy to lose the logic in the midst of all that.
- Scheme provides an alternative mechanism for such cases, the
(cond (test1 exp1)
- The meaning is "Evaluate each test in order. For the first
one that is not false, use the value of corresponding expression."
- For our grades example, we might write
((<= 94 numgrade 100) 'A)
((<= 90 numgrade 93) 'Aminus)
((<= 87 numgrade 89) 'Bplus)
((< numgrade 60) 'F))
- Note that it's much easier to read this
- Note also that there are times in which none of the cases might hol, so we
want a default case. You can use
as the test for the default case, which must come last.
- In order for
cond to be useful, you
need to be able to test for various situations.
- Scheme provides you with a few basic tests and ways of combining tests.
- You can build your ownh tests from those.
- Scheme provides:
- Basic type predicates (is a value a number, symbol, string, list, ....)
- Equality comparators for all types
- Comparators for numeric types
- Three basic mechanisms for combining tests: and, or, and not
- More things that we'll learn about as the semester progresses.
- Do the lab.
- Prepare to reflect on Friday.