Next: , Previous: begin, Up: Control Mechanisms


5.11.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.

— syntax: if test consequent [alternate]

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.

— syntax: cond clause1 clause2 ...

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.

One additional cond-clause is available as an extension to standard Scheme:

          (test guard => expression)
     

where guard and expression must evaluate to procedures. For this clause type, test may return multiple values, and cond ignores its boolean state; instead, cond evaluates guard and applies the resulting procedure to the value(s) of test, as if guard were the consumer argument of call-with-values. Iff the result of that procedure call is a true value, it evaluates expression and applies the resulting procedure to the value(s) of test, in the same manner as the guard was called.

The test of the last clause may be the symbol 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.

— syntax: case key clause1 clause2 ...

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.