# Class 05: Conditionals

Back to Procedures. On to Recursion with Lists.

Held: Wednesday, 28 January 2004

Summary: 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.

Related Pages:

Due

Assignments:

Notes:

• As a change of pace, today's class will primarily focus on group problem solving.

Overview:

• A problem: Turning numbers into letters (in English and Scheme).
• Making life easier: Conditionals.
• What kinds of tests can you use in conditionals?

## A Problem

• 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?

## First Solution: Use `if`

• As you may have noted from your narrative, we are often tempted to describe the procedure in terms of the word if. For example, 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
```(if condition
success-expression
failure-expression)
```
• 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 on efficiency.
• Later, you'll see that this decision affects other things, too.
• For example
```(if (>= grade 94) 'A 'F)
```
• We can nest these conditions.
```(if (>= grade 94)
'A
'Aminus
...)))
```

## Another Solution

• 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
```(cond
((condition1) exp1)
((condition2) exp2)
((condition3) exp3)
...
(else default-exp))
```
• 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.
```(cond
...)
```

## Boolean Expressions

• 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 (`#f`).
• Scheme treats anything that's not false as true.
• We call the tests Boolean expressions in honor of the logician George Boole.
• 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: `number?`, `list?`, and `symbol?`.
• There are many numeric comparators, including `<` (strictly less than), `<=` (less than or equal to), `=` (equal to), `>=` (greater than or equal to), and `>` (strictly greater than).
• There are also a variety of equality comparators, including `=`, `eq?`, `eqv?`, and `equal?`.
• `=` 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 `and`, `or`, and `not`.
• You will have an opportunity to experiment with all of these in the lab.

## History

Back to Procedures. On to Recursion with Lists.

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 Fri May 7 09:43:04 2004.
The source to the document was last modified on Tue Jan 13 10:26:10 2004.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2004S/Outlines/outline.05.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu