CSC 153: Computer Science Fundamentals Grinnell College Spring, 2005 Laboratory Exercise Reading

# Conditional Execution in Scheme

## Abstract

This reading introduces Boolean expressions and reviews how these may be used for executing statements under specified conditions.

## Boolean Values and Expressions

As with many contemporary programming languages, Scheme contains the Boolean values true, written #t, and false, written #f.

The Scheme language allows Boolean values to be combined, using the operators "and", "or", and "not". When these are applied to Boolean values (using Scheme's usual prefix notation), the results follow the conventions from mathematics. Thus,

• (and A B) is true if both A and B are true and false otherwise.
• (or A B) is true (#t) if either A is true or B is true (or both).
• not applied to any true value produces false (#f), and not applied to false (#f) returns true (#t).

More generally, "and" and "or" may take as many arguments as desired. "and" is true if all arguments are true, and "or" is true if any of the arguments is true.

Important Note: Scheme is reasonably liberal in its evaluation of expressions, in that anything that is not explicitly false (#f) is considered true.

## Scheme's Cond Statement

Scheme's cond statement uses expressions to determine what action is to be taken. This is illustrated in the following procedure:
```
(define type-of-number
(lambda (A)
(cond ((< A 0) "the number is negative")
((> A 0) "the number is positive")
(else    "the number is zero")
)
)
)
```
Within this cond expression, (< A 0) is first examined. If it is true, the clause "the number is negative" is evaluated and returned. [Evaluation of the cond expression is finished.] If, however, the expression (< A 0) is false, then evaluation proceeds with the next expression (> A 0). Again, if this expression is true, the following expression "the number is positive" is evaluated, and evaluation of cond is completed. But if this expression also is false, then we move to the next part of cond. else signifies that if we get this far, then the action that follows should be evaluated.

Scheme's if expression uses a conditional expression to determine which of two actions to take. This is illustrated in the following procedure:

```
(define is-negative
(lambda (A)
(if (< A 0)
"the number is negative"
"the number is nonnegative"
)
)
)
```

While cond and if expressions have similar capabilities for branching, note that cond and if have important syntactical differences.

• The cond expression has the general form:
```
(cond  (condition1   action1)
(condition2   action2)
...
(conditionN   actionN)
)
```

In this form, the keyword cond is followed by a sequence of condition and action pairs, and each matched pair is placed in parentheses:

```
(condition     action)
```

As illustrated in the above example, it is common for the last condition −− conditionN −− to be the keyword else, so that the last action −− actionN −− is executed if all of the other conditions are evaluated as false.

• The if expression has the general form:
```
(if   condition
then-part
else-part
)
```

Here, there is only one condition, and this condition follows the keyword if. Thus, the condition is not tied to an action with a separate set of parentheses.

## Choosing Between cond and if

For most purposes, it is recommended that you use the cond expression, rather than an if expression. Of course, if may be used whenever you want to choose between just two alternatives. However, in programming, it is common to begin coding while considering just two options, only to decide that additional choices are needed later on. With a cond expression, adding such conditions is very easy. With a if expression, either you will need to rewrite the work as a cond, or you will need to nest one if within another. Either approach with ifis somewhat cumbersome.

This document is available on the World Wide Web as

```http://www.cs.grinnell.edu/~walker/courses/153.sp05/readings/reading-cond-if.shtml
```

 created February 2, 1997 last revised January 22, 2005  For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.