Are there built-in procedures that take Boolean operands, the way the integer procedures take integer operands?
Just one: the not procedure. Its arity is 1. Given the ``false'' Boolean value, it
returns the ``true'' one, and vice versa:
Actually, Scheme allows the operand to be of any data type. The> (not #f) #t > (not #t) #f
not procedure returns #f if it's given a
non-Boolean value:
Why? Isn't it an error to give a procedure a value of the wrong data type?> (not 5) #f > (not quotient) #f > (not not) #f
It's an error to give it an operand that it's not prepared for, but the
not procedure is required to be prepared for anything. The
main reason for allowing the programmer the freedom to give the procedure a
non-Boolean value is that some Scheme procedures are written in such a way
that if an attempt to compute something is successful, the procedure
returns a non-Boolean result, while if the attempt fails for some unusual
or unforeseen reason, the procedure returns #f.
Indeed, this is a general convention in Scheme: In most contexts where a
Boolean-valued expression would normally appear, one may use an expression
with a value of some other data type, with the understanding that any such
value is understood as ``true,'' while ``false'' is specifically the one
Boolean value expressed by #f.
It seems as if there should be some additional Boolean procedures. What
about and and or? Pascal has built-in operators
under those names.
Scheme too provides and and or, but as special kinds of expressions,
not as procedures.
Next topic
Previous topic
Table of contents
This document is available on the World Wide Web as
http://www.math.grin.edu/~stone/scheme-web/not.html