The Grinnell Scheme Web: The
positive? procedure

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:

> (positive? 11)
#t
> (positive? -4)
#f
> (positive? 0)
#f
The arity of the 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:

(define (zero-or-positive? number)
  (not (negative? number)))
Wouldn't the 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:

> (zero-or-positive? #f)

ERROR: negative?: Wrong type in arg1 #f
When you call the 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


created July 2, 1995
last revised December 29, 1995

Copyright 1995 by John David Stone (stone@math.grin.edu)