5.3. Conditionals


5.3. Conditionals

(if test consequent alternative)

syntax

(if test consequent)

syntax

returns: the value of consequent or alternative depending on the value of test

test, consequent, and alternative are expressions. If no alternative is supplied and test evaluates to false, the result is unspecified.

 (let ((l '(a b c)))   (if (null? l)       '()       (cdr l)))  (b c) (let ((l '()))   (if (null? l)       '()       (cdr l)))  () (let ((abs        (lambda (x)          (if (< x 0)              (- 0 x)              x))))   (abs -4))  4 (let ((x -4))   (if (< x 0)       (list 'minus (- 0 x))       (list 'plus 4)))  (minus 4) 

(not obj )

procedure

returns: #t if obj is false, #f otherwise

not is equivalent to (lambda (x) (if x #f #t)).

 (not #f)  #t (not #t)  #f (not '())  #f (not (< 4 5))  #f 

(and exp ...)

syntax

returns: see explanation

and evaluates its subexpressions in sequence from left to right and stops immediately (without evaluating the remaining expressions) if any expression evaluates to false. The value of the last expression evaluated is returned. A syntax definition of and appears on page 60.

 (let ((x 3))   (and (> x 2) (< x 4)))  #t (let ((x 5))   (and (> x 2) (< x 4)))  #f (and #f '(a b) '(c d))  #f (and '(a b) '(c d) '(e f))  (e f) 

(or exp ...)

syntax

returns: see explanation

or evaluates its subexpressions in sequence from left to right and stops immediately (without evaluating the remaining expressions) if any expression evaluates to a true value. The value of the last expression evaluated is returned. A syntax definition of or appears on page 61.

 (let ((x 3))   (or (< x 2) (> x 4)))  #f (let ((x 5))   (or (< x 2) (> x 4)))  #t (or #f '(a b) '(c d))  (a b) 

(cond clause1 clause2 ...)

syntax

returns: see explanation

Each clause but the last must take one of the forms below.

 (test) (test exp1 exp2 ...) (test => exp) 

The last clause may be in any of the above forms or it may be an "else clause" of the form

 (else exp1 exp2 ...) 

Each test is evaluated in order until one evaluates to a true value or until all of the tests have been evaluated. If the first clause whose test evaluates to a true value is in the first form given above, the value of test is returned.

If the first clause whose test evaluates to a true value is in the second form given above, the expressions exp1 exp2 are evaluated in sequence and the value of the last expression is returned.

If the first clause whose test evaluates to a true value is in the third form given above, the expression exp is evaluated. The value should be a procedure of one argument, which is applied to the value of test. The result of this application is returned.

If none of the tests evaluates to a true value and an else clause is present, the expressions exp1 exp2 of the else clause are evaluated in sequence and the value of the last expression is returned.

If none of the tests evaluates to a true value and no else clause is present, the value is unspecified.

See page 196 for a syntax definition of cond.

 (let ((x 0))   (cond     ((< x 0) (list 'minus (abs x)))     ((> x 0) (list 'plus x))     (else (list 'zero x))))  (zero 0) (define select   (lambda (x)     (cond       ((not (symbol? x)))       ((assq x '((a . 1) (b . 2) (c . 3)))        => cdr)       (else 0)))) (select 3)  #t (select 'b)  2 (select 'e)  0 

(case exp0 clause1 clause2 ...)

syntax

returns: see explanation

Each clause but the last must take the form

 ((key ...) exp1 exp2 ...) 

where each key is a datum distinct from the other keys. The last clause may be in the above form or it may be an else clause of the form

 (else exp1 exp2 ...) 

exp0 is evaluated and the result is compared (using eqv?) against the keys of each clause in order. If a clause containing a matching key is found, the expressions exp1 exp2 are evaluated in sequence and the value of the last expression is returned.

If none of the clauses contains a matching key and an else clause is present, the expressions exp1 exp2 of the else clause are evaluated in sequence and the value of the last expression is returned.

If none of the clauses contains a matching key and no else clause is present, the value is unspecified.

See page 196 for a syntax definition of case

 (let ((x 4) (y 5))   (case (+ x y)     ((1 3 5 7 9) 'odd)     ((0 2 4 6 8) 'even)     (else 'out-of-range)))  odd 




The Scheme Programming Language
The Scheme Programming Language
ISBN: 026251298X
EAN: 2147483647
Year: 2003
Pages: 98

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net