-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-86.scm
44 lines (30 loc) · 1.28 KB
/
2-86.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
43
44
#lang scheme
;Exercise 2.86: Suppose we want to handle complex numbers whose real parts, imaginary parts, magnitudes, and angles can be either ordinary numbers, rational numbers, or other numbers we might wish to add to the system. Describe and implement the changes to the system needed to accommodate this. You will have to define operations such as sine and cosine that are generic over ordinary numbers and rational numbers.
(require "dispatch-table.scm")
(require "modules/sicp/rat.scm")
(require "modules/sicp/sicp.rkt")
(define (make-complex-mag-ang mag ang)
(attach-tag 'complex (cons mag
ang)))
(define (sine x)
(apply-generic 'sine x))
(define (cosine x)
(apply-generic 'cosine x))
(put 'sine '(scheme-number) sin)
(put 'cosine '(scheme-number) cos)
(put 'sine '(rat) (lambda (x) (sin (/ (numer x) (denom x)))))
(put 'cosine '(rat) (lambda (x) (cos (/ (numer x) (denom x)))))
(define (real complex)
(cosine (angle (contents complex))))
(define (imag complex)
(sine (magnitude (contents complex))))
(define (angle complex)
(car complex))
(define (magnitude complex)
(cdr complex))
(define a (make-complex-mag-ang (make-rat 1 2) 2))
(define b (make-complex-mag-ang 0.5 2))
(assert (real a)
(real b))
(assert (imag a)
(imag b))