1.1.7 Scheme conditionals
if
Scheme has an if
procedure:
(if test-expression true-expression false-expression)
test-expression is an expression that returns a boolean
value. If test-expression returns #t
, the if
procedure returns the value of true-expression, otherwise
it returns the value of false-expression.
| guile> (define a 3)
guile> (define b 5)
guile> (if (> a b) "a is greater than b" "a is not greater than b")
"a is not greater than b"
|
cond
Another conditional procedure in scheme is cond
:
(cond (test-expression-1 result-expression-sequence-1)
(test-expression-2 result-expression-sequence-2)
…
(test-expression-n result-expression-sequence-n))
For example:
| guile> (define a 6)
guile> (define b 8)
guile> (cond ((< a b) "a is less than b")
... ((= a b) "a equals b")
... ((> a b) "a is greater than b"))
"a is less than b"
|