www.delorie.com/gnu/docs/guile/r5rs_33.html   search  
 
Buy GNU books!


Revised(5) Scheme

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.2.1 Conditionals

library syntax: cond <clause1> <clause2> ...,

Syntax: Each <clause> should be of the form

 
(<test> <expression1> ...,)

where <test> is any expression. Alternatively, a <clause> may be of the form

 
(<test> => <expression>)

The last <clause> may be an "else clause," which has the form

 
(else <expression1> <expression2> ...,).

Semantics: A `cond' expression is evaluated by evaluating the <test> expressions of successive <clause>s in order until one of them evaluates to a true value (see section see section 6.3.1 Booleans). When a <test> evaluates to a true value, then the remaining <expression>s in its <clause> are evaluated in order, and the result(s) of the last <expression> in the <clause> is(are) returned as the result(s) of the entire `cond' expression. If the selected <clause> contains only the <test> and no <expression>s, then the value of the <test> is returned as the result. If the selected <clause> uses the => alternate form, then the <expression> is evaluated. Its value must be a procedure that accepts one argument; this procedure is then called on the value of the <test> and the value(s) returned by this procedure is(are) returned by the `cond' expression. If all <test>s evaluate to false values, and there is no else clause, then the result of the conditional expression is unspecified; if there is an else clause, then its <expression>s are evaluated, and the value(s) of the last one is(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

library syntax: case <key> <clause1> <clause2> ...,

Syntax: <Key> may be any expression. Each <clause> should have the form

 
((<datum1> ...,) <expression1> <expression2> ...,),

where each <datum> is an external representation of some object. All the <datum>s must be distinct. The last <clause> may be an "else clause," which has the form

 
(else <expression1> <expression2> ...,).

Semantics: A `case' expression is evaluated as follows. <Key> is evaluated and its result is compared against each <datum>. If the result of evaluating <key> is equivalent (in the sense of `eqv?'; see section see section 6.1 Equivalence predicates) to a <datum>, then the expressions in the corresponding <clause> are evaluated from left to right and the result(s) of the last expression in the <clause> is(are) returned as the result(s) of the `case' expression. If the result of evaluating <key> is different from every <datum>, then if there is an else clause its expressions are evaluated and the result(s) of the last is(are) the result(s) of the `case' expression; otherwise the result of the `case' expression is unspecified.

 
(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))                   ==>  consonant

library syntax: and <test1> ...,

The <test> expressions are evaluated from left to right, and the value of the first expression that evaluates to a false value (see section see section 6.3.1 Booleans) is returned. Any remaining expressions are not evaluated. If all the expressions evaluate to true values, the value of the last expression is 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

library syntax: or <test1> ...,

The <test> expressions are evaluated from left to right, and the value of the first expression that evaluates to a true value (see section see section 6.3.1 Booleans) is returned. Any remaining expressions are not evaluated. If all expressions evaluate to false values, the value of the last expression is returned. 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)


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003