Prev Up Next
when and unless are convenient conditionals
to use when only one branch (the ``then'' or the
``else'' branch) of the basic conditional is needed.
(when (< (pressure tube) 60)
(open-valve tube)
(attach floor-pump tube)
(depress floor-pump 5)
(detach floor-pump tube)
(close-valve tube))
Assuming pressure of tube is less than
60, this conditional will attach floor-pump to
tube and depress it 5 times. (attach
and depress are some suitable procedures.)
The same program using if would be:
(if (< (pressure tube) 60)
(begin
(open-valve tube)
(attach floor-pump tube)
(depress floor-pump 5)
(detach floor-pump tube)
(close-valve tube)))
Note that when's branch is an implicit begin,
whereas if requires an explicit begin if either
of its branches has more than one form.
The same behavior can be written using unless as
follows:
(unless (>= (pressure tube) 60)
(open-valve tube)
(attach floor-pump tube)
(depress floor-pump 5)
(detach floor-pump tube)
(close-valve tube))
Not all Schemes provide when and unless.
If your Scheme does not have them, you can
define them as macros (see chap 8).
Prev Up Next