Scheme uses the syntax #t
and #f
for Boolean true and false value, respectively. For example, the
“less-than” function is named <
.
Its result is true if the first argument is less than the second (or, if
there are more than two arguments, that they are in increasing order):
(< 3 4) ⇒ #t (< -3 -4) ⇒ #f (< 2 3 5 7 11)) ⇒ #t
The if
special form takes two or three sub-expressions:
It evaluates the first expression.
If that is true it evaluates the second expression;
otherwise it evaluates the third expression, if provided:
(if (< 3 4) (+ 5 5) (+ 5 6)) ⇒ 10
We call if
a special form rather than a function,
because for a function all the arguments are evaluated before the
function is called, but in a special form that is not neceassarily the case.
In addition to #t
any value except #f
(and the Kawa-specific #!null
)
counts as “true” when evaluating the first expression of an if
.
Unlike C or JavaScript both (zero) and ""
(the empty string) are true:
(if 0 (+ 5 5) (+ 5 6)) ⇒ 10
You can use and
, or
,
and not
to create complex boolean expressions.
Of these and
and or
are special forms that only evaluate as many of the sub-expressions as needed.
(if (not (and (>= i 0) (<= i 9))) (display "error"))
You can use the cond
form as an alternative to
if
:
(cond ((< 3 3) 'greater) ((> 3 3) 'less) (else ’equal)) ⇒ equal
The null value (written as #!null
in Kawa or null
in Java)
is also considered as false.