Go to the first, previous, next, last section, table of contents.

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.

This is really a control construct, not just a logical operator, because whether subexpressions get evaluated depends on the reults 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.

==================================================================
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).)


Go to the first, previous, next, last section, table of contents.