; takes a tag and some contents and creates a ; tagged data element (consisting of the tag ; and the data contents (define (attach-tag type-tag contents) (cons type-tag contents)) ; takes a data item and returns the tag ; associated with the data item. Assume ; the data item is tagged as long as it ; is a pair. (define (type-tag datum) (if (pair? datum) (car datum) (error "TYPE-TAG finds bad datum" datum))) ; takes a data item and returns the content part ; of that data item. Assume the data item is ; tagged as long as it is a pair. (define (contents datum) (if (pair? datum) (cdr datum) (error "CONTENTS finds bad datum" datum))) ; takes a tagged data item and returns ; #t if the datum is tagged rectangular (define (rectangular? z) (eq? (type-tag z) 'rectangular)) ; takes a tagged data item and returns ; #t if the datum is tagged polar (define (polar? z) (eq? (type-tag z) 'polar)) ;; lower level implementation of complex numbers ; RECTANGULAR FORM REPRESENTATION ; takes a real and imaginary part and ; creates a complex number represented ; in rectangular form (define (make-from-real-imag-rectangular x y) (attach-tag 'rectangular (cons x y))) ; given an imaginary number in ; rectangular form ; returns the real part (define (real-part-rectangular z) (car z)) ; given an imaginary number in ; rectangular form ; returns the imaginary part (define (imag-part-rectangular z) (cdr z)) ; given an imaginary number in ; rectangular form ; return the magnitude ; (using trigonomic rels) (define (magnitude-rectangular z) (sqrt (+ (square (real-part-rectangular z)) (square (imag-part-rectangular z))))) ; given an imaginary number in ; rectangular form ; return the angle ; (using trigonomic rels) (define (angle-rectangular z) (atan (imag-part-rectangular z) (real-part-rectangular z))) ; takes a magnigude and an angle and ; creates a complex number represented ; in rectangular form (define (make-from-mag-ang-rectangular r a) (attach-tag 'rectangular (cons (* r (cos a)) (* r (sin a))))) ;; lower level implementation ; POLAR FORM REPRESENTATION ; takes a magnigude and an angle and ; creates a complex number represented ; in polar form (define (make-from-mag-ang-polar r a) (attach-tag 'polar (cons r a))) ; given an imaginary number in ; polar form ; return the magnitude (define (magnitude-polar z) (car z)) ; given an imaginary number in ; rectangular form ; return the angle (define (angle-polar z) (cdr z)) ; given an imaginary number in ; polar form ; returns the real part ; (using trignomic rels) (define (real-part-polar z) (* (magnitude-polar z) (cos (angle-polar z)))) ; given an imaginary number in ; polar form ; returns the imaginary part ; (using trigonomic rels) (define (imag-part-polar z) (* (magnitude-polar z) (sin (angle-polar z)))) ; takes a real and imaginary part and ; creates a complex number represented ; in polar form (harder) (define (make-from-real-imag-polar x y) (attach-tag 'polar (cons (sqrt (+ (square x) (square y))) (atan y x)))) (define (square x) (* x x)) (define c1 (make-from-real-imag-polar 1 2)) (define c2 (make-from-real-imag-rectangular 1 2)) (define c3 (make-from-mag-ang-rectangular 3 4)) (define c4 (make-from-mag-ang-polar 3 4)) ;; **** GENERIC SELECTORS ****** ; takes a tagged complex number and returns ; the real part (define (real-part z) (cond ((rectangular? z) (real-part-rectangular (contents z))) ((polar? z) (real-part-polar (contents z))) (else (error "data type unknown to REAL-PART" z)))) ; takes a tagged complex number and returns ; the imaginary part (define (imag-part z) (cond ((rectangular? z) (imag-part-rectangular (contents z))) ((polar? z) (imag-part-polar (contents z))) (else (error "data-type unknown to IMAG-PART" z)))) ; takes a tagged complex number and returns ; the magnitude (define (magnitude z) (cond ((rectangular? z) (magnitude-rectangular (contents z))) ((polar? z) (magnitude-polar (contents z))) (else (error "data-type unknown to MAGNITUDE" z)))) ; takes a tagged complex number and returns ; the angle (define (angle z) (cond ((rectangular? z) (angle-rectangular (contents z))) ((polar? z) (angle-polar (contents z))) (else (error "data-type unknown to ANGLE" z)))) ; **** GENERIC CONSTRUCTORS THAT TAG ***** ; takes a real part and an imaginary part and ; creates a complex number -- tags it rectangular (define (make-from-real-imag x y) (make-from-real-imag-rectangular x y)) ; takes a magnitude and an angle and creates ; a complex number -- tags it polar (define (make-from-mag-ang r a) (make-from-mag-ang-polar r a))