The Grinnell Scheme Web: The zero?
procedure

So to check whether the value of some expression is zero, would you write (= expression 0)?

You could, but in this special case there's a better way. Scheme provides a one-operand procedure xero? for exactly this purpose:

> (zero? 0)
#t
> (zero? -0)
#t
> (zero? 0.0)
#t
> (zero? -3)
#f
> (zero? (quotient 5 7))
#t
The arity of the zero? procedure is 1, and the operand must be an integer, a rational, a real number, or a complex number.

Why is calling the zero? procedure better than calling the = procedure with a second operand of 0?

In some implementations, it's faster (and it shouldn't be slower in any implementation). For human readers, it's a little easier to read.

Are there any other special cases? Is there a predicate that tests whether a number is equal to one, for instance?

Not a predefined one. You could, of course, define your own.

What's so special about zero, then? Why does it merit a predicate of its own?

Empiricially, the test for equality with zero comes up more frequently in real-world programs than tests for equality with any other number, so it would make sense for a Scheme implementor to optimize this case carefully. The design of many central processing units facilitate this optimization by providing a hardware instruction that tests for equality with zero.

Mathematically, zero is special because it is the base case for mathematical induction over the natural numbers, because it is the origin of the real line and of the complex plane, and because it is the identity for addition and the zero for multiplication. (Of course, these are the main reasons why the test for equality with zero occurs so frequently in real-world programs.)


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/zero-ques.html


created July 2, 1995
last revised December 30, 1995

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