Next: , Previous: , Up: baselib expressions   [Index]


4.4.5 Derived conditionals

Syntax: cond ?cond-clause1 ?cond-clause2
Auxiliary Syntax: =>
Auxiliary Syntax: else

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:

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 no else 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
Syntax: case ?key ?case-clause1 ?case-clause
Auxiliary Syntax: =>
Auxiliary Syntax: else

?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:

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 no else 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)
Syntax: and ?test1

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.

Syntax: or ?test1

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: , Previous: , Up: baselib expressions   [Index]