The standard boolean objects for true and false are written as #t and #f.
What really matters,
though, are the objects that the Scheme conditional expressions (if,
cond, and, or, when, unless, do)
treat as true or
false. The phrase “a true value” (or sometimes just “true”)
means any object treated as true by the conditional expressions, and the phrase “a false value” (or “false”) means any
object treated as false by the conditional expressions.
Of all the Scheme values, only #f counts as false in conditional
expressions. All other Scheme values, including #t,
count as true. A test-expression is an expression evaluated
in this manner for whether it is true or false.
test-expression ::= expression
consequent ::= expression
alternate ::= expression
Syntax: if test-expressionconsequentalternate
Syntax: if test-expressionconsequent
An
ifexpression is evaluated as follows: first,test-expressionis evaluated. If it yields a true value, thenconsequentis evaluated and its values are returned. Otherwisealternateis evaluated and its values are returned. Iftestyields#fand noalternateis specified, then the result of the expression is unspecified.(if (> 3 2) 'yes 'no) ⇒ yes (if (> 2 3) 'yes 'no) ⇒ no (if (> 3 2) (- 3 2) (+ 3 2)) ⇒ 1 (if #f #f) ⇒ unspecifiedThe
consequentandalternateexpressions are in tail context if theifexpression itself is.
cond-clause ::= (test-expression expression …)
| (test => expression)
Syntax: cond cond-clausecond-clause…
Syntax: cond cond-clause… (else expression…)
A
condexpression is evaluated by evaluating thetest-expressions of successivecond-clauses in order until one of them evaluates to a true value. When atest-expressionevaluates to a true value, then the remainingexpressions in itscond-clauseare evaluated in order, and the results of the lastexpressionin thecond-clauseare returned as the results of the entirecondexpression. If the selectedcond-clausecontains only thetest-expressionand noexpressions, then the value of thetest-expressionis returned as the result. If the selectedcond-clauseuses the=>alternate form, then theexpressionis evaluated. Its value must be a procedure. This procedure should accept one argument; it is called on the value of thetest-expressionand the values returned by this procedure are returned by thecondexpression.If all
test-expressions evaluate to#f, and there is noelseclause, then the conditional expression returns unspecified values; if there is anelseclause, then itsexpressions are evaluated, and the values of the last one are returned.(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)) ⇒ 2For a
cond-clauseof one of the following forms:(testexpression…) (elseexpressionexpression…)the last
expressionis in tail context if thecondform itself is. For acond clauseof the form:(test=>expression)the (implied) call to the procedure that results from the evaluation of
expressionis in a tail context if thecondform itself is.
Syntax: case keycase-clausecase-clause…
keymust be an expression.
case-clause::=((datum…)expressionexpression…)
| (elseexpressionexpression…)
The second form, which specifies an “
elseclause”, may only appear as the lastcase-clause. Eachdatumis an external representation of some object. The data represented by thedatums need not be distinct.A
caseexpression is evaluated as follows.
keyis evaluated and its result is compared usingeqv?against the data represented by thedatums of eachcase clausein turn, proceeding in order from left to right through the set of clauses.If the result of evaluating
keyis equivalent to a datum of acase clause, the correspondingexpressions are evaluated from left to right and the results of the last expression in thecase clauseare returned as the results of thecaseexpression. Otherwise, the comparison process continues.If the result of evaluating
keyis different from every datum in each set, then if there is anelseclause its expressions are evaluated and the results of the last are the results of thecaseexpression; otherwise thecaseexpression returns unspecified values.(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 'consonant)) ⇒ consonantThe last
expressionof acase clauseis in tail context if thecaseexpression itself is.
Syntax: and test-expression…
If there are no
test-expressions,#tis returned. Otherwise, thetest-expressionare evaluated from left to right until atest-expressionreturns#for the lasttest-expressionis reached. In the former case, theandexpression returns#fwithout 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) ⇒ #tThe
andkeyword could be defined in terms ofifusingsyntax-rulesas follows:(define-syntax and (syntax-rules () ((and) #t) ((and test) test) ((and test1 test2 ...) (if test1 (and test2 ...) #t))))The last
test-expressionis in tail context if theandexpression itself is.
Syntax: or test-expression…
If there are no
test-expressions,#fis returned. Otherwise, thetest-expressions are evaluated from left to right until atest-expressionreturns a true valuevalor the lasttest-expressionis reached. In the former case, theorexpression returnsvalwithout 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
orkeyword could be defined in terms ofifusingsyntax-rulesas 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-expressionis in tail context if theorexpression itself is.
Syntax: when test-expressionform...
If
test-expressionis true, evaluate eachformin order, returning the value of the last one.
Syntax: unless test-expressionform...
If
test-expressionis false, evaluate eachformin order, returning the value of the last one.