SICP Exercise 2.80
Question
Define a generic predicate =zero? that tests if its argument is zero, and
install it in the generic arithmetic package. This operation should work for
ordinary numbers, rational numbers, and complex numbers.
Answer
First, we need to define the generic function:
(define (=zero? x) (apply-generic 'zero x))
Then, we must add it to our three installation packages: the simple numbers, rational numbers and complex numbers.
A simple number is zero if it is equal to
; scheme numbers
(put '=zero? '(scheme-number)
(λ (x) (tag (= x 0))))
; rational numbers
(put '=zero? '(rational)
(λ (x) (tag (= 0 (numer x)))))
; complex numbers
(put '=zero? '(complex)
(λ (x) (tag (and (= 0 (real-part x))
(= 0 (imag-part x))))))