-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex-msg.rkt
32 lines (27 loc) · 996 Bytes
/
complex-msg.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
#lang racket
(require "numerical.rkt")
(provide make-from-real-imag make-from-mag-ang real-part imag-part magnitude angle)
(define (make-from-real-imag x y)
(define (dispatch op)
(cond ((eq? op 'real-part) x)
((eq? op 'imag-part) y)
((eq? op 'magnitude)
(sqrt (+ (square x) (square y))))
((eq? op 'angle) (atan y x))
(else (error "Unknown op - MAKE-FROM-REAL-IMAG" op))))
dispatch)
(define (make-from-mag-ang r a)
(define (dispatch op)
(cond ((eq? op 'magnitude) r)
((eq? op 'angle) a)
((eq? op 'real-part)
(* r (cos a)))
((eq? op 'imag-part)
(* r (sin a)))
(else (error "Unknown op - MAKE-FROM-MAG-ANG" op))))
dispatch)
(define (apply-generic op arg) (arg op))
(define (real-part z) (apply-generic 'real-part z))
(define (imag-part z) (apply-generic 'imag-part z))
(define (magnitude z) (apply-generic 'magnitude z))
(define (angle z) (apply-generic 'angle z))