chickadee » scheme » cond

(cond <clause[1]> <clause[2]> ...)syntax

Syntax: Each <clause> should be of the form

(<test> <expression[1]> ...)

where <test> is any expression. Alternatively, a <clause> may be of the form

(<test> => <expression>)

The last <clause> may be an "else clause," which has the form

(else <expression[1]> <expression[2]> ...).

Semantics: A cond expression is evaluated by evaluating the <test> expressions of successive <clause>s in order until one of them evaluates to a true value (see the section about booleans below). When a <test> evaluates to a true value, then the remaining <expression>s in its <clause> are evaluated in order, and the result(s) of the last <expression> in the <clause> is(are) returned as the result(s) of the entire cond expression. If the selected <clause> contains only the <test> and no <expression>s, then the value of the <test> is returned as the result. If the selected <clause> uses the => alternate form, then the <expression> is evaluated. Its value must be a procedure that accepts one argument; this procedure is then called on the value of the <test> and the value(s) returned by this procedure is(are) returned by the cond expression. If all <test>s evaluate to false values, and there is no else clause, then the result of the conditional expression is unspecified; if there is an else clause, then its <expression>s are evaluated, and the value(s) of the last one is(are) returned.

(cond ((> 3 2) 'greater)
      ((< 3 2) 'less))           ===>  greater
(cond ((> 3 3) 'greater)
      ((< 3 3) 'less)
      (else 'equal))             ===>  equal
(cond ((assv 'b '((a 1) (b 2))) => cadr)
      (else #f))                 ===>  2

As an extension to R5RS, CHICKEN also supports the SRFI-61 syntax:

(<generator> <guard> => <expression>)

In this situation, generator is always evaluated. Its resulting value(s) are used as argument(s) for the guard procedure. Finally, if guard returns a non-#f value, the expression is evaluated by calling it with the result of guard. Otherwise, evaluation procedes to the next clause.