练习2-79
首先需要加载第125页的apply-generic函数,然后添加题中要求的通用型操作equ?。
(define (equ? x y)
(apply-generic ‘equ? x y))
题目要求我们能够处理常规的数、有理数和复数。分别列出如下。
常规数:
(define (install-scheme-number-package)
(define (tag x)
(attach-tag 'scheme-number x))
(put 'make 'scheme-number
(lambda (x)
(tag x)))
(put 'equ? '(scheme-number scheme-number)
(lambda (x y)
(= x y)))
'done)
(define (make-scheme-number n)
((get 'make 'scheme-number) n))
有理数:
define (install-rational-package)
(define (numer x)
(car x))
(define (denom x)
(cdr x))
(define (make-rat n d)
(let ((g (gcd n d)))
(cons (/ n g) (/ d g))))
(define (tag x)
(attach-tag 'rational x))
(put 'make 'rational
(lambda (n d)
(tag (make-rat n d))))
(put 'equ? '(rational rational)
(lambda (x y)
(and (= (numer x) (numer y))
(= (denom x) (denom y)))))
'done)
(define (make-rational n d)
((get 'make 'rational) n d))
复数:
(define (install-complex-package)
(define (make-from-real-imag x y)
((get 'make-from-real-imag 'rectangular) x y))
(define (make-from-mag-ang r a)
((get 'make-from-mag-ang 'polar) r a))
(define (tag z)
(attach-tag 'complex z))
(put 'make-from-real-imag 'complex
(lambda (x y)
(tag (make-from-real-imag x y))))
(put 'make-from-mag-ang 'complex
(lambda (r a)
(tag (make-from-mag-ang r a))))
(put 'equ? '(complex complex)
(lambda (x y)
(and (= (real-part x) (real-part y))
(= (imag-part x) (imag-part y)))))
'done)
(define (make-complex-from-real-imag x y)
((get 'make-from-real-imag 'complex) x y))
(define (make-complex-from-mag-ang r a)
((get 'make-from-mag-ang 'complex) r a))
练习2-80
和上一题一样,先加载第125页的apply-generic函数,然后添加题中要求的通用型操作=zero?。
(define (=zero? x)
(apply-generic ‘=zero? x))
常规数:
(define (install-scheme-number-package)
(define (tag x)
(attach-tag 'scheme-number x))
(put 'make 'scheme-number
(lambda (x)
(tag x)))
(put '=zero? '(scheme-number)
(lambda (value)
(= value 0)))
'done)
(define (make-scheme-number n)
((get 'make 'scheme-number) n))
有理数:
(define (install-rational-package)
(define (numer x)
(car x))
(define (denom x)
(cdr x))
(define (make-rat n d)
(let ((g (gcd n d)))
(cons (/ n g) (/ d g))))
(define (tag x)
(attach-tag 'rational x))
(put 'make 'rational
(lambda (n d)
(tag (make-rat n d))))
(put '=zero? '(rational)
(lambda (r)
(= 0 (numer r))))
'done)
(define (make-rational n d)
((get 'make 'rational) n d))
复数:
(define (install-complex-package)
(define (make-from-real-imag x y)
((get 'make-from-real-imag 'rectangular) x y))
(define (make-from-mag-ang r a)
((get 'make-from-mag-ang 'polar) r a))
(define (tag z)
(attach-tag 'complex z))
(put 'make-from-real-imag 'complex
(lambda (x y)
(tag (make-from-real-imag x y))))
(put 'make-from-mag-ang 'complex
(lambda (r a)
(tag (make-from-mag-ang r a))))
(put '=zero? '(complex)
(lambda (c)
(and (= 0 (real-part c))
(= 0 (imag-part c)))))
'done)
(define (make-complex-from-real-imag x y)
((get 'make-from-real-imag 'complex) x y))
(define (make-complex-from-mag-ang r a)
((get 'make-from-mag-ang 'complex) r a))
感谢访问,希望对您有所帮助。 欢迎关注或收藏、评论或点赞。
为使本文得到斧正和提问,转载请注明出处:
http://blog.csdn.net/nomasp