Conditionals
(cond
{clause1} {clause 2} ...
)
syntax
else
auxiliary syntax
=>
auxiliary syntax
Syntax: Clauses take one of two forms, either
({test} {expression1} ...)
where test is any expression, or
({test} => {expression})
The last clause can be an "else clause," which has the form
(else {expression1} {expression2} ...)
Semantics:
A
If the selected clause contains only the
test and no expressions, then the value of the
test is returned as the result. If the selected clause uses the
If all tests evaluate
to
(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
(case
{key} {clause1} {clause2} ...
)
syntax
Syntax:
Key can be any expression. Each clause has
the form
(({datum1} ...) {expression1} {expression2} ...)
where each datum is an external representation of some object. It is an error if any of the datums are the same anywhere in the expression. Alternatively, a clause can be of the form
(({datum1} ...) => {expression})
The last clause can be an "else clause," which has one of the forms
(else {expression1} {expression2} ...)
or
(else => {expression})
Semantics:
A
If the result of
evaluating key is different from every datum, then if
there is an else clause, its expressions are evaluated and the
results of the last are the results of the
If the selected clause or else clause uses the
(case (* 2 3)
((2 3 5 7) 'prime)
((1 4 6 8 9) 'composite)) ==> composite
(case (car '(c d))
((a) 'a)
((b) 'b)) ==> unspecified
(case (car '(c d))
((a e i o u) 'vowel)
((w y) 'semivowel)
(else => (lambda (x) x))) ==> c
(and
{test1} ...
)
syntax
Semantics:
The test expressions are evaluated from left to right, and if
any expression evaluates to #f (see
section 6.3), then #f is returned. Any remaining
expressions are not evaluated. If all the expressions evaluate to
true values, the values of the last expression are 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
(or
{test1} ...
)
syntax
Semantics:
The test expressions are evaluated from left to right, and the value of the
first expression that evaluates to a true value (see
section 6.3) is returned. Any remaining expressions
are not evaluated. If all expressions evaluate to #f
or 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)
(when
{test} {expression1} {expression2} ...
)
syntax
Syntax:
The test is an expression.
Semantics:
The test is evaluated, and if it evaluates to a true value,
the expressions are evaluated in order. The result of the when
expression is unspecified.
(when (= 1 1.0)
(display "1")
(display "2")) ==> unspecified and prints 12
(unless
{test} {expression1} {expression2} ...
)
syntax
Syntax:
The test is an expression.
Semantics:
The test is evaluated, and if it evaluates to
(unless (= 1 1.0)
(display "1")
(display "2")) ==> unspecified and prints nothing
(cond-expand
{ce-clause1} {ce-clause2} ...
)
syntax
Syntax:
The cond-expand expression type
provides a way to statically
expand different expressions depending on the
implementation. A
ce-clause takes the following form:
({feature requirement} {expression} ...)
The last clause can be an "else clause," which has the form
(else {expression})
A feature requirement takes one of the following forms:
Semantics:
Each implementation maintains a list of feature identifiers which are
present, as well as a list of libraries which can be imported. The
value of a feature requirement is determined by replacing
each feature identifier and cond-expand is then expanded by evaluating the
feature requirements of successive ce-clauses
in order until one of them returns #t . When a true clause is
found, the corresponding expressions are expanded to a
begin , and the remaining clauses are ignored.
If none of the feature requirements evaluate to #t , then
if there is an else clause, its expressions are
included. Otherwise, the behavior of the cond-expand is unspecified.
Unlike cond , cond-expand does not depend on the value
of any variables.
The exact features provided are implementation-defined, but for portability a core set of features is given in appendix B. |