[ < ] [ > ] [ << ] [ Up ] [ >> ] [Top] [Contents] [Index] [ ? ]

## 4.5 Conditionals

Special Form: if test consequent alternative
Special Form: if test consequent
[R5RS] Test is evaluated. If it yields a true value, consequent is evaluated. Otherwise, alternative is evaluated. If alternative is not provided, it results undefined value.

 (if (number? 3) 'yes 'no) => yes (if (number? #f) 'yes 'no) => no (let ((x '(1 . 2))) (if (pair? x) (values (car x) (cdr x)) (values #f #f))) => 1 and 2

Special Form: cond clause1 clause2 ...
[R5RS] Each clause must be the form
 (test expr ...) (test => expr) (else expr expr2 ...)
The last form can appear only as the last clause.

cond evaluates test of each clauses in order, until it yields a true value. Once it yields true, if the clause is the first form, the corresponding exprs are evaluated and the result(s) of last expr is(are) returned; if the clause is the second form, the expr is evaluated and it must yield a procedure that takes one argument. Then the result of test is passed to it, and the result(s) it returns will be returned. If no test yields true, and the last clause is not the third form (else clause), an undefined value is returned.

If the last clause is else clause and all tests are failed, exprs in the else clause are evaluated, and its last expr's result(s) 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

Special Form: case key clause1 clause2 ...
[R5RS] Key may be any expression. Each clause should have the form
 ((datum ...) expr expr2 ...)
where each datum is an external representation of some object. All the datums must be distinct. The last clause may be an "else clause," which has the form
 (else expr expr2 ...)

First, key is evaluated and its result is compared against each datum. If the result of evaluating datum is equivalent (using eqv?, See section 6.1 Equivalence), to a datum, then the expressions in the corresponding clause are evaluated sequentially, and the result(s) of the last expression in the clause is(are) returned from the case expression. If the result of evaluating key is different from every datum, then if there is an else clause its expressions are evaluated and the result(s) of the last is(are) the result(s) of the case expression; otherwise the result of the case expression is unspecified.
 (case (* 2 3) ((2 3 5 7) 'prime) ((1 4 6 8 9) 'composite)) => composite (case (car '(c d)) ((a) 'a) ((b) 'b)) => undefined (case (car '(c d)) ((a e i o u) 'vowel) ((w y) 'semivowel) (else 'consonant)) => consonant

Special Form: and test ...
[R5RS] The test expressions are evaluated from left to right, and the value of the first expression that evaluates to a false value is returned. Any remaining expressions are not evaluated. If all the expressions evaluate to true values, the value of the last expression is returned. If there are no expressions then #t is returned.

 (and (= 2 2) (> 2 1)) => #t (and (= 2 2) (< 2 1)) => #f (and 1 2 'c '(f g)) => (f g) (and) => #t

Special Form: or test ...
[R5RS] The test expressions are evaluated from left to right, and the value of the first expression that evaluates to a true value is returned. Any remaining expressions are not evaluated. If all expressions evaluate to false values, the value of the last expression is returned. If there are no expressions then #f is returned.

 (or (= 2 2) (> 2 1)) => #t (or (= 2 2) (< 2 1)) => #t (or #f #f #f) => #f (or (memq 'b '(a b c)) (/ 3 0)) => (b c)

Special Form: when test body ...
Special Form: unless test body ...
Evaluates test. If it yields true value (or false in case of unless), body ... are evaluated sequentially, and the result(s) of the last evaluation is(are) returned. Otherwise, undefined value is returned.

 [ < ] [ > ] [ << ] [ Up ] [ >> ] [Top] [Contents] [Index] [ ? ]

This document was generated by Ken Dickey on November, 28 2002 using texi2html