Introduction
Using the raise operation of exercise 2.83, modify the apply-generic procedure so that it coerces its arguments to have the same type by the method of successive raising, as discussed in this section. You will need to devise a way to test which of two types is higher in the tower. Do this in a manner that is "compatible'' with the rest of the system and will not lead to problems in adding new levels to the tower.
Discussion
(define tower '(integer rational real complex))
(define (tower-pos symbol tower)
(if (= (car tower) symbol)
0
(+ 1 (tower-pos symbol (cdr tower)))))
(define (apply-generic op . args)
(let ((type-tags (map type-tag args)))
(let ((proc (get op type-tags)))
(if proc
(apply proc (map contents args))
(if (= (length args) 2)
(let ((type1 (car type-tags))
(type2 (cadr type-tags))
(a1 (car args))
(a2 (cadr args)))
(cond
((< (tower-pos type1 tower) (tower-pos type2 tower))
(apply-generic op (raise (car args)) (cadr args)))
((> (tower-pos type1 tower) (tower-pos type2 tower))
(apply-generic op (car args) (raise (cadr args))))
(else (error "No method for these types"
(list op type-tags)))))
(error "No method for these types"
(list op type-tags)))))))
Comments