When Scheme encounters a procedure call, it looks at all of the subexpressions within the parentheses and evaluates each one. Sometimes, however, the programmer wants Scheme to exercise more discretion -- specifically, to select just one subexpression for evaluation from two or more alternatives. In such cases, one uses a conditional expression -- that is, an expression that tests whether some condition is met and selects the subexpression to evaluate on the basis of the outcome of that test.
For instance, let's write a procedure to compute the disparity
between two given real numbers -- the amount by which one of them exceeds
the other. We can compute this by subtraction, but before we can do the
subtraction we need to know which of the two given numbers (let's call them
fore and aft) is greater, so that we can make it
the minuend and the other the subtrahend.
If fore is greater, that is, we compute the disparity by
evaluating the expression (- fore aft); otherwise, the
expression we need is (- aft fore). A conditional expression
-- specifically, an if-expression -- selects one or
the other of these expressions, depending on the outcome of a test, thus:
(if (> fore aft) ; If FORE is greater than AFT ...
(- fore aft) ; ... subtract AFT from FORE ...
(- aft fore)) ; ... and otherwise subtract FORE from AFT.
Here is the complete definition of the disparity procedure:
;;; disparity: compute the amount by which one real number
;;; exceeds another
;;; John David Stone
;;; Department of Mathematics and Computer Science
;;; Grinnell College
;;; stone@cs.grinnell.edu
;;; created January 27, 2000
last revised March 17, 2000
;;; Givens:
;;; FORE and AFT, both real numbers.
;;; Result:
;;; EXCESS, a real number.
;;; Preconditions:
;;; None.
;;; Postcondition:
;;; The greater of FORE and AFT is equal to the sum of EXCESS
;;; and the lesser of FORE and AFT.
(define disparity
(lambda (fore aft)
(if (> fore aft)
(- fore aft)
(- aft fore))))
In an if-expression, the test (the expression
following the keyword if) is always evaluated first. If its
value is #t, then the consequent (the expression
following the test) is evaluated, and the alternate (the
expression following the consequent) is ignored. On the other hand, if the
value of the test is #f, then the consequent is ignored and
the alternate is evaluated.
Scheme accepts if-expressions in which the value of the test
is non-Boolean. However, all non-Boolean values are classified as
``truish'' and cause the evaluation of the consequent.
Define and test a Scheme procedure neighbor that takes one
argument, an integer, and returns the next higher integer if its argument
is even, the next lower integer if its argument is odd. (Start by writing
a comment that describes the purpose of the procedure.)
Define and test a Scheme procedure type-of-list that takes one
argument, a list, and returns the symbol empty if the argument
is the empty list, the symbol non-empty otherwise.
When there are more than two alternatives, it is often more convenient to
set them out using a cond-expression. Like
if, cond is a keyword. It is followed by zero or
more lists of expressions called cond-clauses. The
first expression within a cond-clause is a test, similar to
the test in an if-expression. When the value of such a test
is found to be #f, the subexpressions that follow the test are
ignored and Scheme proceeds to the test at the beginning of the next
cond-clause. But when a test is evaluated and the value turns
out to be true, or even ``truish'' -- anything other than #f
-- each of the remaining expressions in the same cond-clause
is evaluated in turn, and the value of the last one becomes the value of
the entire cond-expression. Subsequent
cond-clauses are completely ignored.
In other words, when Scheme encounters a cond-expression, it
works its way through the cond-clauses, evaluating the test at
the beginning of each one, until it reaches a test that succeeds
(one that does not have #f as its value). It then makes a
ninety-degree turn and evaluates the other expressions in the selected
cond-clause, retaining the value of the last expression.
If all of the tests in a cond-expression are found to be
false, the value of the cond-expression is unspecified (that
is, it might be anything!). To prevent the surprising results that can
ensue when one computes with unspecified values, good programmers
customarily end every cond-expression with a
cond-clause in which the keyword else appears in
place of a test. Scheme treats such a cond-clause as if it
had a test that always succeeded. If it is reached, the subexpressions
following else are evaluated, and the value of the last one
is the value of the whole cond-expression.
For example, here is a cond-expression that inspects a list
called ls:
(cond ((null? ls) 'none)
((symbol? (car ls)) 'first)
(else 'other))
The expression has three cond-clauses. In the first, the test
is (null? ls). If ls happens to be the empty
list, the value of this first test is #t, so we evaluate
whatever comes after the test to find the value of the entire expression --
in this case, the symbol none
If ls is not the empty list, then we proceed instead to the
second cond-clause. Its test is (symbol? (car
ls)) -- in other words, ``look at the first element of
ls and determine whether it is a symbol.'' If it is, then
again we evaluate whatever comes after the test and obtain the symbol
first.
However, if the first element of ls is not a symbol, then we
proceed instead to the third cond-clause. Since the keyword
else appears in this cond-clause in place of a
test, we take that as an automatic success and evaluate
'other, so that that value of the whole
cond-expression in this case is the symbol other.
Define and test a Scheme procedure report-victory that takes
one argument, a real number, and returns the string "I won!"
if that number is positive, the string "You won!" if it is
negative, and the string "It's a tie!" if it is zero.
Define a Scheme procedure classify-by-age that takes one
argument, a non-negative integer, and returns the symbol
infant if the argument is 0 or 1, the symbol
child if the argument is less than or equal to 12, the symbol
adolescent if the argument is less than or equal to 18, the
symbol adult if the argument is less than 65, and the symbol
senior in any other case. Test your procedure by calling it
with a representative age within each classification.
If-expressions and cond-expressions are two
examples of control structures in Scheme -- expressions that
control the order in which their subexpressions are evaluated or the
circumstances under which they are evaluated. Scheme provides two other
control structures that do conditional evaluation: the
and-expression and the or-expression.
In an and-expression, the expressions that follow the keyword
and are evaluated in succession until one is found to have the
value #f (in which case the rest of the expressions are
skipped and the #f becomes the value of the entire
and-expression) or all of the expressions have been evaluated
(in which case the value of the last expression becomes the value of the
and-expression). This gives the programmer a way to combine
several tests into one that will succeed only if all of its parts succeed.
In an or-expression, the expressions that follow the keyword
or are evaluated in succession until one is found to have a
value other than#f (in which case the rest of the expressions
are skipped and this value becomes the value of the entire
or-expression) or all of the expressions have been evaluated
(the value of the or-expression is #f). This
gives the programmer a way to combine several tests into one that will
succeed if any of its parts succeeds.
Define and test a Scheme predicate string-or-symbol? that
takes one argument and returns #t if the argument is either a
string or a symbol, #f if it is neither.
Define and test a Scheme predicate between? that takes three
arguments, all real numbers, and determines whether the second one lies
strictly between the first and third (returning #t if it is,
#f if it is not). For example, 6 lies strictly between 5 and
13, so both (between? 5 6 13) and
(between? 13 6 5) should have the value
#t.
Three line segments can be assembled into a triangle if, and only
if, the length of each of them is less than the sum of the lengths of the
other two. Define a Scheme predicate triangle? that takes
three arguments, all positive real numbers, and determines whether line
segments of those three lengths (assumed to be measured in the same units)
could be assembled into a triangle.
This document is available on the World Wide Web as
http://www.cs.grinnell.edu/~stone/courses/scheme/conditional-evaluation.xhtml
created September 9, 1997
last revised March 17, 2000