<< Previous exercise (2.76)
| Index |
Next exercise (2.78) >>
Before Alyssa added the code, there is no magnitude operation for complex,hence the error. apply-generic is invoked twice, first dispatch is magnitude of complex, second is magnitude of rectangular.
There is some mistake here. There is no procedure magnitude for the 'complex package, so the first thing failing would be:
(put 'magnitude '(complex) magnitude)
In the code of the book the package does not contain such a procedure magnitude. It seems to assume that Alyssa also writes that code, but in the exercise 2.77 does not say so. The book also does not state in which package the code should be added. The specific procedures magnitude inside the packages rectangular and polar do not work for complex, because they assume, that they are working with either representation and are not aware of the additional tag 'complex attached to the data. So they would extract the wrong pieces from the data and then fail.
The implementation of magnitude inside the complex package would probably have to unwrap the tagged data by one tag, the complex tag, and perform a lookup in the table of operations itself:
(define (magnitude z) (get 'magnitude (cadr z) (cdr z)))
;; get operation for magnitude of the data wrapped inside the data.
;; (cadr z) would become 'rectangular
;; (cdr z) would become (list 'rectangular (cons 3 4))
So basically with the information given in the book the tracing (as demanded in the exercise) of the calls would fail (correct me if I am wrong about this please).
However, under reasonable assumptions, like a defined magnitude in the package for complex, I think the answer of meteorgan is correct.
Perhaps this is what you're looking for. The generic magnitude selector is defined in section 2.4.3:
(define (magnitude z) (apply-generic 'magnitude z))