-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.81-coercion-same-type.rkt
51 lines (41 loc) · 1.33 KB
/
2.81-coercion-same-type.rkt
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
43
44
45
46
47
48
49
50
51
#lang racket
(require racket/trace
racket/file
"../tag.rkt")
(require "../put-get-coercion.rkt")
(require "apply-generic.rkt")
(require (file "../2.5.1-Generic-Arithmetic-Operations/2.77-apply-generic-magnitude.rkt"))
(define (scheme-number->scheme-number n) n)
(define (complex->complex z) z)
(define (scheme-number->complex n)
(make-complex-from-real-imag (contents n) 0))
(put-coercion 'scheme-number
'scheme-number
scheme-number->scheme-number)
(put-coercion 'complex 'complex complex->complex)
(put-coercion 'scheme-number
'complex
scheme-number->complex)
(define (install-complex-package)
(define (make-from-real-imag x y)
((get 'make-from-real-imag 'rectangular) x y))
(define (tag z) (attach-tag 'complex z))
(put 'make-from-real-imag 'complex
(lambda (x y) (tag (make-from-real-imag x y))))
'done)
(define (install-scheme-number-package)
(define (tag x)
(attach-tag 'scheme-number x))
(put 'make 'scheme-number
(lambda (x) (tag x)))
(put 'add '(scheme-number scheme-number)
(lambda (x y) (tag (+ x y))))
(put 'exp '(scheme-number scheme-number)
(lambda (x y) (tag (expt x y))))
'done)
(install-complex-package)
(install-scheme-number-package)
(trace-define (exp x y) (apply-generic 'exp x y))
(define z1 (make-complex-from-real-imag 3 4))
(define z2 (make-complex-from-real-imag 5 6))
(exp z1 2)