1.1.6 Conditional Expressions and Predicates
- View
- Revisions

The expressive power of the class of procedures that we can define at this point is very limited, because we have no way to make tests and to perform different operations depending on the result of a test. For instance, we cannot define a procedure that computes the absolute value of a number by testing whether the number is positive, negative, or zero and taking different actions in the different cases according to the rule

This construct is called a case analysis, and there is a special form in Lisp for notating such a case analysis. It is called cond
(which stands for “conditional”), and it is used as follows:
(define (abs x)
(cond ((> x 0) x)
((= x 0) 0)
((< x 0) (- x))))
The general form of a conditional expression is
(cond (1> 1>)
(2> 2>)
⋮
(n> n>))
consisting of the symbol cond
followed by parenthesized pairs of expressions (
called clauses. The first expression in each pair is a predicate — that is, an expression whose value is interpreted as either true or false.[17]
Conditional expressions are evaluated as follows. The predicate 1> 2> 2> 3>cond> is undefined.
The word predicate is used for procedures that return true or false, as well as for expressions that evaluate to true or false. The absolute-value procedure abs
makes use of the primitive predicates >
, <
, and =
.[18] These take two numbers as arguments and test whether the first number is, respectively, greater than, less than, or equal to the second number, returning true or false accordingly.
Another way to write the absolute-value procedure is
(define (abs x)
(cond ((< x 0) (- x))
(else x)))
which could be expressed in English as “If x is less than zero return - x; otherwise return x.” Else
is a special symbol that can be used in place of the in the final clause of a cond
. This causes the cond
to return as its value the value of the corresponding
Here is yet another way to write the absolute-value procedure:
(define (abs x)
(if (< x 0)
(- x)
x))
This uses the special form if
, a restricted type of conditional that can be used when there are precisely two cases in the case analysis. The general form of an if> expression is
(if )
To evaluate an if
expression, the interpreter starts by evaluating the <predicate> part of the expression. If the
In addition to primitive predicates such as <
, =
, and >
, there are logical composition operations, which enable us to construct compound predicates. The three most frequently used are these:
-
(and
1> ...n> )The interpreter evaluates the expressions
one at a time, in left-to-right order. If any evaluates to false, the value of the and
expression is false, and the rest of the’s are not evaluated. If all ’s evaluate to true values, the value of the and
expression is the value of the last one. -
(or
1> ...n> )The interpreter evaluates the expressions
one at a time, in left-to-right order. If any evaluates to a true value, that value is returned as the value of the or
expression, and the rest of the’s are not evaluated. If all ’s evaluate to false, the value of the or
expression is false. - (not
) The value of a
not
expression is true when the expressionevaluates to false, and false otherwise.
Notice that and
and or
are special forms, not procedures, because the subexpressions are not necessarily all evaluated. Not
is an ordinary procedure.
As an example of how these are used, the condition that a number x be in the range 5 < x < 10 may be expressed as
(and (> x 5) (< x 10))
As another example, we can define a predicate to test whether one number is greater than or equal to another as
(define (>= x y)
(or (> x y) (= x y)))
or alternatively as
(define (>= x y)
(not (< x y)))
#t
and #f
. When the interpreter checks a predicate’s value, it interprets #f as false. Any other value is treated as true. (Thus, providing #t
is logically unnecessary, but it is convenient.) In this book we will use names true
and false
, which are associated with the values #t
and #f
respectively. [back]
Abs
also uses the “minus” operator -
, which, when used with a single operand, as in (- x)
, indicates negation. [back]
if
and cond
is that the cond
clause may be a sequence of expressions. If the corresponding is found to be true, the expressions cond
. In an if expression, however, the Exercises
Comments
Post new comment