Is the syntax of the even? procedure exactly that that of
the odd? procedure?
Yes, they are completely analogous:
The arity of the> (even? 8) #t > (even? -7) #f > (even? 0) #t > (even? -6.1) ERROR: even?: Wrong type in arg1 -6.1
even? procedure is 1, and the operand must be
an integer.
So why does Scheme have both procedures? If you had odd?,
couldn't you just define even? as its negation?
Sure. In fact, that's the way it works in Pascal -- the odd-parity test is predefined, and if you want the even-parity test you define it yourself. My guess is that the designers of Scheme felt that taking either predicate as ``more primitive'' than the other was tantamount to dabbling in mystical numerology. Since there's no clear reason for preferring one to the other, they gave us both.
That seems a little strange. Don't you want to start with some kind of minimal set of predefined procedures, to keep the language as simple as possible?
If you did that, you wouldn't predefine either odd?
or even?, since you can define both of them in terms of
procedures that we've already seen:
On the other hand, once the designers decided to add a predefined parity predicate to the language, it would have been a false economy to put one in without the other: Which one is ``primitive'' and which one had to be included from the programmer's library of stock procedures would have been just one more detail for her to remember. It's better this way.(define (even? number) (zero? (modulo number 2))) (define (odd? number) (= (modulo number 2) 1))
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/even-ques.html