Chapter 2, Building Abstractions with Data
Section - 2.5 - Systems with Generic Operations
Exercise 2.80
This is almost similar to the previous exercise. The changes required in each package is shown below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
;; number package:
(put '=zero? '(scheme-number scheme-number) (lambda (x) (= x 0)))
;; rational package:
(define (=zero? x)
(= (numer x) 0)
)
(put '=zero? '(rational) =zero?)
;; complex package:
(define (=zero? z)
(and
(= (apply-generic 'real-part z) 0)
(= (apply-generic 'imag-part z) 0)
)
)
(put '=zero? '(complex) =zero?)
; generic definition
(define (=zero? x)
(apply-generic '=zero? x)
)