Node:if cond case, Next:, Previous:begin, Up:Control Mechanisms

26.2 Simple Conditional Evaluation

Guile provides three syntactic constructs for conditional evaluation. if is the normal if-then-else expression (with an optional else branch), cond is a conditional expression with multiple branches and case branches if an expression has one of a set of constant values.

if test consequent [alternate] syntax
All arguments may be arbitrary expressions. First, test is evaluated. If it returns a true value, the expression consequent is evaluated and alternate is ignored. If test evaluates to #f, alternate is evaluated instead. The value of the evaluated branch (consequent or alternate) is returned as the value of the if expression.

When alternate is omitted and the test evaluates to #f, the value of the expression is not specified.

cond clause1 clause2 ... syntax
Each cond-clause must look like this:
(test expression ...)

where test and expression are arbitrary expression, or like this

(test => expression

where expression must evaluate to a procedure.

The tests of the clauses are evaluated in order and as soon as one of them evaluates to a true values, the corresponding expressions are evaluated in order and the last value is returned as the value of the cond-expression. For the => clause type, expression is evaluated and the resulting procedure is applied to the value of test. The result of this procedure application is then the result of the cond-expression.

The test of the last clause may be the keyword else. Then, if none of the preceding tests is true, the expressions following the else are evaluated to produce the result of the cond-expression.

case key clause1 clause2 ... syntax
key may be any expression, the clauses must have the form
((datum1 ...) expr1 expr2 ...)

and the last clause may have the form

(else expr1 expr2 ...)

All datums must be distinct. First, key is evaluated. The the result of this evaluation is compared against all datums using eqv?. When this comparison succeeds, the expression(s) following the datum are evaluated from left to right, returning the value of the last expression as the result of the case expression.

If the key matches no datum and there is an else-clause, the expressions following the else are evaluated. If there is no such clause, the result of the expression is unspecified.