乐正

Actions speak louder than words.

Sicp-ex2-79

问题

请定义一个通用型相等谓词equ?,它能检查两个数是否相等。请将它安装到通用算数包里。 这一操作应该能处理常规的数、有理数和复数。

解答

练习2.79 (ex2-79.scm) download
1
2
3
4
5
6
7
8
9
10
11
12
(define (equ? x y) (apply-generic 'equ? x y))

(define (install-equ?-package)
  (put 'equ? '(scheme-number scheme-number)
       (lambda (x y) (= x y)))
  (put 'equ? '(rational rational)
       (lambda (x y) (and (= (numer x) (numer y))
                          (= (denom x) (denom y)))))
  (put 'equ? '(complex complex)
       (lambda (x y) (and (= (real-part x) (real-part y))
                          (= (imag-part x) (imag-part y)))))
  'done)

draft

« sicp-ex2-78 sicp-ex2-80 »

Comments