乐正

Actions speak louder than words.

Sicp-ex2-80

问题

请定义一个通用谓词=zero?,检查其参数是否为0,并将它安装到通用算术包里。这一操作 应该能够处理常规的数、有理数和复数。

解答

练习2.80 (ex2-80.scm) download
1
2
3
4
5
6
7
8
9
10
11
(define (=zero? x) (apply-generic '=zero? x))

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

draft

« sicp-ex2-79 sicp-ex2-81 »

Comments