Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Complex numbers III
- Data-directed programming
- This solves two problems:
- 1. The constructor and selector functions had to know both
- representations in order to decide what to do.
- 2. The underlying names had to be disambiguated. So for instance,
- we needed
- real-part-rectangular
- real-part-polar
- both defined in order to finally define real-part in terms of
- them.
- With data-directed programming, neither of these problems occurs.
- (define (square t) (* t t))
- (define (atan2 y x)(atan (/ y x))) ;; UMB Scheme doesn't have (atan y x)
- (define (install-rectangular-package)
- ;; internal procedures
- (define (real-part z) (car z))
- (define (imag-part z) (cdr z))
- (define (make-from-real-imag x y) (cons x y))
- (define (magnitude z)
- (sqrt (+ (square (real-part z))
- (square (imag-part z)))))
- (define (angle z)
- (atan2 (imag-part z) (real-part z)))
- (define (make-from-mag-ang r a)
- (cons (* r (cos a)) (* r (sin a))))
- ;; interface to the rest of the system
- (define (tag x) (attach-tag 'rectangular x))
- (put 'real-part 'rectangular real-part)
- (put 'imag-part 'rectangular imag-part)
- (put 'magnitude 'rectangular magnitude)
- (put 'angle 'rectangular angle)
- (put 'make-from-real-imag 'rectangular
- (lambda (x y) (tag (make-from-real-imag x y))))
- (put 'make-from-mag-ang 'rectangular
- (lambda (r a) (tag (make-from-mag-ang r a))))
- 'done)
- (define (install-polar-package)
- ;; internal procedures
- (define (magnitude z) (car z))
- (define (angle z) (cdr z))
- (define (make-from-mag-ang r a) (cons r a))
- (define (real-part z)
- (* (magnitude z) (cos (angle z))))
- (define (imag-part z)
- (* (magnitude z) (sin (angle z))))
- (define (make-from-real-imag x y)
- (cons (sqrt (+ (square x) (square y)))
- (atan2 y x)))
- ;; interface to the rest of the system
- (define (tag x) (attach-tag 'polar x))
- (put 'real-part 'polar real-part)
- (put 'imag-part 'polar imag-part)
- (put 'magnitude 'polar magnitude)
- (put 'angle 'polar angle)
- (put 'make-from-real-imag 'polar
- (lambda (x y) (tag (make-from-real-imag x y))))
- (put 'make-from-mag-ang 'polar
- (lambda (r a) (tag (make-from-mag-ang r a))))
- 'done)
- (define (apply-generic op arg)
- (let ((proc (get op (type-tag arg))))
- (if proc
- (proc (contents arg))
- (error
- "No method for this type -- APPLY-GENERIC"
- (list op (type-tag arg))))))
- ;; Generic selectors
- (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))
- ;; Constructors for complex numbers
- (define (make-from-real-imag x y)
- ((get 'make-from-real-imag 'rectangular) x y))
- (define (make-from-mag-ang r a)
- ((get 'make-from-mag-ang 'polar) r a))
- ;; ****************** MESSAGE PASSING ******************
- (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) (atan2 y x))
- (else
- (error "Unknown op -- MAKE-FROM-REAL-IMAG" op))))
- dispatch)
- (define (apply-generic op arg) (arg op))
Add Comment
Please, Sign In to add comment