and
and or
The special forms and
and or
can be used as logical operators,
but they can also be used as control structures, which is why they are
special forms.
and
takes any number of expressions, and evaluates them in
sequence, until one of them returns #f
or all of them
have been evaluated. At the point where one returns #f
, and
returns that value as the value of the and
expression. If none of
them returns #f
, it returns the value of the last subexpression.
An expression with and
is really a control construct, not just a
logical operator, because whether subexpressions get evaluated depends
on the results of the previous subexpressions.
and
is often used to express both control flow and value returning,
like a sequence of if
tests. You can write something like
(and (try-first-thing) (try-second-thing) (try-third-thing))
If the three calls all return true values, and
returns the value
of the last one. If any of them returns #f
, however, none of
the rest are evaluated, and #f
is returned as the value of the
overall expression.
Likewise, or
takes any number of arguments, and returns the value
of the first one that returns a true value (i.e., anything but #f
).
It stops when it gets a true value, and returns it without evaluating
the remaining subexpressions.
(or (try-first-thing) (try-second-thing) (try-third-thing))
or
keeps trying subexpressions until one of them does return
a true value; if that happens, or
stops and returns that value.
If none of them returns anything but #f
, it returns #f
.
not
is just a procedure
not
is a procedure that takes one argument, which may be
any kind of Scheme value, and returns #t
or #f
. If
the argument value is #f (the unique false object), it returns
#t
, and otherwise returns #f
. That is, all values
count as true except for the false object--just as in a conditional.
For example, (not 0)
returns #f
.
Given that and
and or
are special forms, you might
think that the logical not
operator is a special form
as well. It isn't. It's just a procedure--in particular, a
predicate.
That not
is a procedure makes sense because not
always
evaluates its (one) argument, and returns a value. It doesn't treat any
arguments specially--it's just a normal first-class procedure, whose
argument is evaluated in the usual way before the procedure is actually
called.
In general, operations that can be procedures are procedures.
Scheme only has special forms for things that are actually special,
and need their arguments treated differently from arguments to
procedure calls. (Even Scheme's most powerful control construct,
call-with-current-continuation
, is just a first-class
procedure.)
================================================================== This is the end of Hunk A. TIME TO TRY IT OUT At this point, you should go read Hunk B of the next chapter and work through the examples using a running Scheme system. Then return here and resume this chapter. ==================================================================
(Go to Hunk B, which starts at section An Interactive Programming Environment (Hunk B).)