Node: Conditionals, Next: , Previous: Procedures, Up: Expressions



Conditionals

if <test> <consequent> <alternate> R5RS
if <test> <consequent> R5RS
An if expression is evaluated as follows: first, <test> is evaluated. If it yields a true value, then <consequent> is evaluated and its value(s) is(are) returned. Otherwise <alternate> is evaluated and its value(s) is(are) returned. If <test> yields a false value and no <alternate> is specified, then the result of the expression is void.
            (if (> 3 2) 'yes 'no)           =>  yes
            (if (> 2 3) 'yes 'no)           =>  no
            (if (> 3 2)
                (- 3 2)
                (+ 3 2))                    =>  1
          

cond <clause1> <clause2> ... R5RS
In a cond, 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> ...)
          

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 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 void; 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
          

case <key> <clause1> <clause2> ... R5RS
In a case, 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> ...).
          

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?) 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 void.

            (case (* 2 3)
              ((2 3 5 7) 'prime)
              ((1 4 6 8 9) 'composite))     =>  composite
            (case (car '(c d))
              ((a) 'a)
              ((b) 'b))                     =>  void
            (case (car '(c d))
              ((a e i o u) 'vowel)
              ((w y) 'semivowel)
              (else 'consonant))            =>  consonant
          

and <test1> ... R5RS
The <test> expressions are evaluated from left to right, and the value of the first expression that evaluates to a false value 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
          

or <test1> ... R5RS
The <test> expressions are evaluated from left to right, and the value of the first expression that evaluates to a true value 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)
          

when <test> <expression1> <expression2> ... STKLOS Syntax
If the <test> expression yields a true value, the <expression>s are evaluated from left to right and the value of the last <expression> is returned. Otherwise, when returns void

unless <test> <expression1> <expression2> ... STKLOS Syntax
If the <test> expression yields a false value, the <expression>s are evaluated from left to right and the value of the last <expression> is returned. Otherwise, unless returns void