-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-80.scm
42 lines (29 loc) · 889 Bytes
/
2-80.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#lang scheme
(require "modules/sicp/sicp.rkt")
(require "dispatch-table.scm")
(define (num rat)
(car rat))
(define (denom rat)
(cdr rat))
(define (real complex)
(car complex))
(define (imag complex)
(cdr complex))
(define (make-rat num den)
(attach-tag '(rat) (cons num den)))
(define (make-complex-real-imag real imag)
(attach-tag '(complex) (cons real imag)))
(define (=zero? num)
(apply-generic '=zero? num))
(put '=zero? '(scheme-number) (lambda (x) (= x 0)))
(put '=zero? '(rat) (lambda (rat) (=zero? (num rat))))
(put '=zero? '(complex) (lambda (comp) (and (=zero? (real comp))
(=zero? (imag comp)))))
(assert (=zero? 0)
true)
(assert (=zero? (make-rat 0 2))
true)
(assert (=zero? (make-rat (make-rat 0 1) 2))
true)
(assert (=zero? (make-complex-real-imag (make-rat 0 2) 0))
true)