How do you determine the sign of a given integer?
It depends on how you classify zero. To find out whether an integer is
strictly positive (thus grouping zero with the negative integers), call the
positive? procedure:
The arity of the> (positive? 11) #t > (positive? -4) #f > (positive? 0) #f
positive? procedure is 1, and the operand
must be an integer, a rational, or a real number. And if I want to classify zero along with the positive numbers?
Then you'd call the negative?
procedure instead, and reverse the result. You could even define a
procedure that would do the reversing for you:
Wouldn't the(define (zero-or-positive? number) (not (negative? number)))
zero-or-positive? procedure, so defined,
return the ``true'' Boolean value for things other than real numbers? What
would happen if you gave it, say, a Boolean operand? Let's try it:
When you call the> (zero-or-positive? #f) ERROR: negative?: Wrong type in arg1 #f
zero-or-positive? procedure, the procedure
itself calls the negative? procedure, which requires a
numerical operand. Since the value of number is a Boolean,
the call to negative? crashes. So the
zero-or-positive? procedure imposes the same precondition on
its operand as negative?.
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/positive-ques.html