diff options
Diffstat (limited to 'coding-exercises/2/78/complex-polar.rkt')
| -rw-r--r-- | coding-exercises/2/78/complex-polar.rkt | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/coding-exercises/2/78/complex-polar.rkt b/coding-exercises/2/78/complex-polar.rkt index 725f387..41a81a1 100644 --- a/coding-exercises/2/78/complex-polar.rkt +++ b/coding-exercises/2/78/complex-polar.rkt @@ -2,20 +2,42 @@ (provide install-polar-package) (require "../../../shared/data-directed-programming.rkt") -(define (install-polar-package apply-generic put) +(define (install-polar-package apply-fn put) + ;; import methods + (define (mul a b) + (apply-fn 'mul a b)) + (define (cos a) + (apply-fn 'cos a)) + (define (sin a) + (apply-fn 'sin a)) + (define (sqr a) + (apply-fn 'sqr a)) + (define (sqrt a) + (apply-fn 'sqrt a)) + (define (atan a b) + (apply-fn 'atan a b)) + + ;; selectors (define (magnitude z) - (car z)) - (define (angle z) - (cdr z)) - (define (make-from-mag-ang r a) - (cons r a)) + (car z) + (define (angle z) + (cdr z)) + (define (make-from-mag-ang r a) + (cons r a))) + + ;; generic selectors (define (real-part z) - (apply-generic 'mul (magnitude z) (apply-generic 'cos (angle z)))) + (mul (magnitude z) + (cos (angle z)))) (define (imag-part z) - (apply-generic 'mul (magnitude z) (apply-generic 'sin (angle z)))) + (mul (magnitude z) + (sin (angle z)))) + + ;; constructor (define (make-from-real-imag x y) (cons (sqrt (+ (sqr x) (sqr y))) (atan y x))) + ;; register in data-driven package) (define (typtag x) (attach-tag 'polar x)) |
