2015/10/31

SICP 問題 2.77

(put 'real-part '(complex) real-part)
(put 'imag-part '(complex) imag-part)
(put 'magnitude '(complex) magnitude)
(put 'angle '(complex) angle)


;; magnitudeはcomplex型を知らないのでerrorを返す.
;; なので表にcomplex型を追加すれば動く.


(magnitude z)
;=>
(magnitude (complex ractangular 3 . 4))
;=>
(apply-generic magnitude (complex ractangular 3 . 4))
;=>
((get 'magnitude '(complex)) (ractangular 3 . 4))
;=>
(magnitude (ractangular 3 . 4))
;=>
(apply-generic magnitude (ractangular 3 . 4))
;=>
((get 'magnitude '(ractangular)) (3 . 4))
;=>
(magnitude (3 . 4))
;=>
(sqrt (+ (square 3) (square 4)))
;=>
5

© 2022 wat-aro