Next: baselib expressions binding, Previous: baselib expressions assignments, Up: baselib expressions [Index]
Each ?cond-clause must be of the form:
(?test ?expression1 …)
where ?test is an expression. Alternatively, a ?cond-clause may be of the form:
(?test => ?expression)
The last ?cond-clause may be an else
clause, which has the form:
(else ?expression1 ?expression2 …)
When the a cond
syntax use has the else
form, a cond
expression is
evaluated as follows:
cond
expression.
=>
alternate form, then the ?expression is evaluated. Its value must be a procedure.
This procedure should accept one argument; it is called on the value of the ?test and the
values returned by this procedure are returned by the cond
expression.
#f
then ?expressions in the else
clause are
evaluated, and the values of the last one are returned.
(?test ?expression1 …) (else ?expression1 ?expression2 …)
the last ?expression is in tail context if the cond
form itself is. For a
?cond-clause of the form:
(?test => ?expression)
the (implied) call to the procedure that results from the evaluation of ?expression is in a
tail context if the cond
form itself is.
When the a cond
syntax use does not have the else
form, a cond
expression is evaluated as above but the return values are always discarded; such a cond
syntax use always returns zero values.
NOTE The specification for the syntax use without
else
clause breaks compliance with R6RS. According to the standard: when noelse
is present, the expression returns either the return values of the selected clause, or unspecified values if all the ?test expressions return#f
.
Usage examples:
(cond ((> 3 2) 'greater) ((< 3 2) 'less)) ⇒ greater (cond ((> 3 3) 'greater) ((< 3 3) 'less) (else 'equal)) ⇒ equal (cond ('(1 2 3) => cadr) (else #f)) ⇒ 2
?key must be an expression. Each ?case-clause must have one of the following forms:
((?datum1 …) ?expression1 ?expression2 …) (else ?expression1 ?expression2 …) ((?datum1 …) => ?expression) (else => ?expression)
The arrow forms are only available in non–strict R6RS mode, --no-strict-r6rs. The else
clause may only appear as the last
?case-clause. Each ?datum is an external representation of some object. The data
represented by the ?datums need not be distinct.
When the else
clause is present, a case
expression is evaluated as follows:
eqv?
against the data represented
by the ?datums of each ?case-clause in turn, proceeding in order from left to right
through the set of clauses.
case
expression.
Otherwise, the comparison process continues.
=>
alternate form, then the ?expression
is evaluated; its value must be a procedure; this procedure should accept one argument. It is
called on the value of the ?key and the values returned by this procedure are returned by the
case
expression.
else
clause are evaluated and the results of the last are the results of
the case
expression
case
expression itself is.
When the a case
syntax use does not have the else
form, a case
expression is evaluated as above but the return values are always discarded; such a case
syntax use always returns zero values.
NOTE The specification for the syntax use without
else
clause breaks compliance with R6RS. According to the standard: when noelse
is present, the expression returns either the return values of the selected clause, or unspecified values if the result of evaluating ?key is different from every datum in each set.
Usage examples:
(case (* 2 3) ((2 3 5 7) 'prime) ((1 4 6 8 9) 'composite)) ⇒ composite (case (car '(c d)) ((a) 'a) ((b) 'b)) ⇒ no values (case (car '(c d)) ((a e i o u) 'vowel) ((w y) 'semivowel) (else 'consonant)) ⇒ consonant (case 2 ((a b c) 'symbol) ((1 2 3) => (lambda (N) (vector N))) (else 'else)) ⇒ #(2) (case 9 ((a b c) 'symbol) ((1 2 3) 'number) (else => (lambda (N) (vector N)))) ⇒ #(9)
The ?tests must be expressions.
If there are no ?tests, #t
is returned. Otherwise, the
?test expressions are evaluated from left to right until a
?test returns #f
or the last ?test is reached. In the
former case, the and
expression returns #f
without
evaluating the remaining expressions. In the latter case, the last
expression is evaluated and its values are returned.
(and (= 2 2) (> 2 1)) ⇒ #t (and (= 2 2) (< 2 1)) ⇒ #f (and 1 2 'c '(f g)) ⇒ (f g) (and) ⇒ #t
The and
keyword could be defined in terms of if
using
syntax-rules
as follows:
(define-syntax and (syntax-rules () ((and) #t) ((and test) test) ((and test1 test2 ...) (if test1 (and test2 ...) #t))))
The last ?test expression is in tail context if the and
expression itself is.
The ?tests must be expressions.
If there are no ?tests, #f
is returned. Otherwise, the
?test expressions are evaluated from left to right until a
?test returns a true value val or the last ?test is
reached. In the former case, the or
expression returns val
without evaluating the remaining expressions. In the latter case, the
last expression is evaluated and its values are returned.
(or (= 2 2) (> 2 1)) ⇒ #t (or (= 2 2) (< 2 1)) ⇒ #t (or #f #f #f) ⇒ #f (or '(b c) (/ 3 0)) ⇒ (b c)
The or
keyword could be defined in terms of if
using
syntax-rules
as follows:
(define-syntax or (syntax-rules () ((or) #f) ((or test) test) ((or test1 test2 ...) (let ((x test1)) (if x x (or test2 ...))))))
The last ?test expression is in tail context if the or
expression itself is.
Next: baselib expressions binding, Previous: baselib expressions assignments, Up: baselib expressions [Index]