Class 08: Conditionals Lab

Back to Procedure Definitions. On to Pause for Breath.

Held Wednesday, September 11, 2002

Summary

Today we continue our study of conditionals by working with them experimentally.

Due

Assignments

Notes:

• 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 lab.
• Almost everyone turned in HW1 long before class.
• The times some of you sent your homework were depressing.
• "Site" and "Cite" are different words.

Overview

Conditionals

• 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 or counter-clockwise.
• Scheme provides a number of tools for dealing with these kinds of decisions.
• The simplest is `if`
• The normal form for `if` is
`(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 value.
• 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 so forth.
• We can write that with a lot of nested `if` expressions.
```(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` expression.
```(cond (test1 exp1)
(test2 exp2)
...
(testn expn))
```
• 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
```(cond
...
```
• 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 `else` or `#t` as the test for the default case, which must come last.

Things You Can Test

• In order for `if` and `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.

Conditionals Lab

• Do the lab.
• Prepare to reflect on Friday.

History

Thursday, 29 August 2002

Back to Procedure Definitions. On to Pause for Breath.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Mon Dec 2 08:40:55 2002.
The source to the document was last modified on Tue Sep 3 23:13:28 2002.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Outlines/outline.08.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu