問題解答
問2.81
a. apply-generic 内で proc が見つかった時点で処理は終了するが、引数の型同士の演算が定義されていない場合に apply-generic が再帰的に呼ばれるようになってしまい処理がおわらない。
b. 同じ型の引数の強制型変換って、意味を感じられないような。。。?そもそも a. で無限ループすることが分かっているので意味がないどころかダメってことで FA?
c.ちょっと手抜き感はあるけど。
(define (apply-generic op . args)
(let ((type-tags (map type-tags 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)))
(let ((t1->t2 (get-coercioin type1 type2))
(t2->t1 (get-coercioin type2 type1)))
(cond (eq? type1 type2)
(apply proc (map contents args))
(t1->t2
(apply-generic op (t1->t2 a1) a2))
(t2->t1
(apply-generic op (t2->t1 a2)))
(else
(error "No method for these types"
(list op type-tags))))))
(error "No method for these types"
(list op type-tags)))))))