=> Table of Contents | Section 4.1 - Expressions | Section 4.2.2 - Binding constructs

4.2 Derived expression types

The constructs in this section are hygienic, as discussed in section 4.3. For reference purposes, section 7.3 gives syntax definitions that will convert most of the constructs described in this section into the primitive constructs described in the previous section.

4.2.1 Conditionals

(cond   ...)     syntax
else                               auxiliary syntax
=>                                 auxiliary syntax

Syntax: take one of two forms, either

(  ...)

where is any expression, or

( => )

The last can be an “else clause,” which has the form

(else   ...)

Semantics: A cond expression is evaluated by evaluating the expressions of successive s in order until one of them evaluates to a true value (see section 6.3). When a evaluates to a true value, the remaining s in its are evaluated in order, and the results of the last in the are returned as the results of the entire cond expression.

If the selected contains only the and no s, then the value of the is returned as the result. If the selected uses the => alternate form, then the is evaluated. It is an error if its value is not a procedure that accepts one argument. This procedure is then called on the value of the and the values returned by this procedure are returned by the cond expression.

If all s evaluate to #f, and there is no else clause, then the result of the conditional expression is unspecified; if there is an else clause, then its s are evaluated in order, 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 ((assv 'b '((a 1) (b 2))) => cadr)
      (else #f))          ⇒ 2
(case    ...)     syntax

Syntax: can be any expression. Each has the form:

(( ...)   ...)

where each is an external representation of some object. It is an error if any of the s are the same anywhere in the expression. Alternatively, a can be of the form:

(( ...) => )

The last can be an “else clause,” which has one of the forms:

(else   ...)

or

(else => )

Semantics: A case expression is evaluated as follows. is evaluated and its result is compared against each . If the result of evaluating is the same (in the sense of eqv?; see section 6.1) to a , then the expressions in the corresponding are evaluated in order and the results of the last expression in the are returned as the results of the case expression.

If the result of evaluating is different from every , then if there is an else clause, its expressions are evaluated and the results of the last are the results of the case expression; otherwise the result of the case expression is unspecified.

If the selected or else clause uses the => alternate form, then the is evaluated. It is an error if its value is not a procedure accepting one argument. This procedure is then called on the value of the and the values returned by this procedure are returned by the case expression.

(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  ...)    syntax

Semantics: The 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  ...)     syntax

Semantics: The 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    ...)     syntax

Syntax: The 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    ...)

Syntax: The is an expression.

Semantics: The test is evaluated, and if it evaluates to #f, the expressions are evaluated in order. The result of the unless expression is unspecified.

(unless (= 1 1.0)
  (display "1")
  (display "2"))  ⇒ unspecified and prints nothing
(cond-expand   ...)

Syntax: The cond-expand expression type provides a way to statically expand different expressions depending on the implementation. A takes the following form:

(  ...)

The last clause can be an “else clause,” which has the form:

(else  ...)

A 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 is determined by replacing each and (library ) on the implementation’s lists with #t, and all other feature identifiers and library names with #f, then evaluating the resulting expression as a Scheme boolean expression under the normal interpretation of and, or, and not.

A cond-expand is then expanded by evaluating the s of successive s in order until one of them returns #t. When a true clause is found, the corresponding s are expanded to a begin, and the remaining clauses are ignored. If none of the s evaluate to #t, then if there is an else clause, its s 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.

=> Section 4.2.2 - Binding constructs

Proxy Information
Original URL
gemini://shaggypeak.com/library/r7rs/section4.2.gmi
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
508.381598 milliseconds
Gemini-to-HTML Time
1.892796 milliseconds

This content has been proxied by September (3851b).