-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-85.scm
40 lines (30 loc) · 962 Bytes
/
2-85.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
#lang scheme
(require "dispatch-table.scm")
(require "modules/sicp/sicp.rkt")
(require "2-79.scm")
(require "modules/sicp/rat.scm")
(require "complex.scm")
(put-coercion 'complex 'rat (lambda (comp)
(make-rat (real (contents comp)) 1)))
(put-coercion 'rat 'scheme-number (lambda (rat)
(numer (contents rat))))
(define (push-type type)
(let ((down-types (cdr (memq type reversed-types-tower))))
(if (pair? down-types)
(car down-types)
nil)))
(assert (push-type 'rat)
'scheme-number)
(assert (push-type 'scheme-number)
nil)
(define (drop arg)
(let ((pushed ((get-coercion (type-tag arg) (push-type (type-tag arg))) arg)))
(if (equ? arg (raise pushed))
pushed
arg)))
(assert (drop (make-rat 3 1))
3)
(assert (drop (make-complex-real-imag 3 0))
(make-rat 3 1))
(assert (drop (make-rat 3 2))
(make-rat 3 2))