This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1Version of October 8, 1996MASSACHVSETTS INSTITVTE OF TECHNOLOGYDepartment of Electrical Engineering and Computer Science6.001|Structure and Interpretation of Computer ProgramsFall Semester, 1996Lecture Notes, Octob er 8 { Generic Op erationsSome Strategies for Managing Data ComplexityComplex Numb ers - Rectangular and Polar Co ordinates6.001, Fall Semester, 1996|Lecture Notes, Octob er 8 { Generic Op erations2Complex Number Arithmetic(define (add-complex z1 z2)(make-from-real-imag (+ (real-part z1) (real-part z2))(+ (imag-part z1) (imag-part z2))))(define (sub-complex z1 z2)(make-from-real-imag (- (real-part z1) (real-part z2))(- (imag-part z1) (imag-part z2))))(define (mul-complex z1 z2)(make-from-mag-ang (* (magnitude z1) (magnitude z2))(+ (angle z1) (angle z2))))(define (div-complex z1 z2)(make-from-mag-ang (/ (magnitude z1) (magnitude z2))(- (angle z1) (angle z2))))Alternative Implementations;;;Ben's representation { using rectangular co ordinates;;;RepRect = Sch-Num X Sch-Num(define (make-from-real-imag x y) (cons x y))(define (real-part z) (car z))(define (imag-part z) (cdr z))(define (magnitude z) (sqrt (+ (square (real-part z))(square (imag-part z)))))(define (angle z) (atan (imag-part z) (real-part z)))(define (make-from-mag-ang r a)(cons (* r (cos a)) (* r (sin a))));;;Alyssa's representation { using p olar co ordinates;;;RepPolar = Sch-Num X Sch-Num(define (make-from-mag-ang r a) (cons r a))(define (magnitude z) (car z))(define (angle z) (cdr z))(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))) (atan y x)))6.001, Fall Semester, 1996|Lecture Notes, Octob er 8 { Generic Op erations3Tagged Data Conventions(define (attach-tag type-tag contents)(cons type-tag contents))(define (type-tag datum)(if (pair? datum)(car datum)(error "Bad tagged datum - TYPE-TAG" datum)))(define (contents datum)(if (pair? datum)(cdr datum)(error "Bad tagged datum - CONTENTS" datum)))Complex Numb ers with Tagged Data;;Complex Number Typ e Predicates(define (rectangular? z) (eq? (type-tag z) 'rectangular))(define (polar? z) (eq? (type-tag z) 'polar));;;Ben's representation { Rectangular = 'rectangular X RepRect(define (make-from-real-imag x y)(attach-tag 'rectangular (cons x y)))(define (make-from-mag-ang r a)(attach-tag 'rectangular (cons (* r (cos a)) (* r (sin a)))));; Rectangular -> Sch-Num(define (real-part-rectangular z) (car (contents z)))(define (imag-part-rectangular z) (cdr (contents z)))(define (magnitude-rectangular z)(sqrt (+ (square (real-part-rectangular z))(square (imag-part-rectangular z)))))(define (angle-rectangular z)(atan (imag-part-rectangular z)(real-part-rectangular z)));;;Alyssa's representation { Polar = 'p olar X RepPolar(define (make-from-mag-ang r a) (attach-tag 'polar (cons r a)))(define (make-from-real-imag x y)(attach-tag 'pola (cons (sqrt (+ (square x) (square y)))(atan y x))));; Polar -> Sch-Num(define (magnitude-polar z) (car (contents z)))(define (angle-polar z) (cdr (contents z)))(define (real-part-polar z) (* (magnitude-polar z) (cos (angle-polar z))))(define (imag-part-polar z) (* (magnitude-polar z) (sin (angle-polar z))))6.001, Fall Semester, 1996|Lecture Notes, Octob er 8 { Generic Op erations4Corresp onding Generic Op erators & Constructors;; Complex = Rectangular U Polar;; accessors: Complex -> Sch-Num(define (real-part z)(cond ((rectangular? z) (real-part-rectangular z))((polar? z) (real-part-polar z))(else (error "Unknown type - REAL-PART" z))))(define (imag-part z)(cond ((rectangular? z) (imag-part-rectangular z))((polar? z) (imag-part-polar z))(else (error "Unknown type - IMAG-PART" z))))(define (magnitude z)(cond ((rectangular? z) (magnitude-rectangular z))((polar? z) (magnitude-polar z))(else (error "Unknown-type - MAGNITUDE" z))))(define (angle z)(cond ((rectangular? z) (angle-rectangular z))((polar? z) (angle-polar z))(else (error "Unknown-type - ANGLE" z))));; make-from-real-imag: Sch-Num, Sch-Num -> Complex(define (make-from-real-imag x y)(make-from-real-imag-rectangular x y));; make-from-mag-ang: Sch-Num, Sch-Num -> Complex(define (make-from-mag-ang r a)(make-from-mag-ang-polar r a))Problems with this Approach6.001, Fall Semester, 1996|Lecture Notes, Octob er 8 { Generic Op erations5Generic Interface - Table Driven(put <op> <type> <procedure>)(get <op> <type>)(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));; A simple version...(define (simple-apply-generic op arg)(let ((type-tag (type-tag arg)))(let ((proc (get op type-tag)))(if proc(apply proc (list arg))(error "No method for types - APPLY-GENERIC"(list op type-tag))))));; Version to support variable number of arguments:(define (multiple-apply-generic op . args)(let ((type-tags (map type-tags args)))(let ((proc (get op type-tags)))(if proc(apply proc args)(error "No method for types - APPLY-GENERIC"(list op type-tags))))));; Convention: Generic system manages type tags.(define (apply-generic op . args)(let ((type-tags (map type-tags args)))(let ((proc (get op type-tags)))(if proc(apply proc (map contents args))(error "No method for types - APPLY-GENERIC"(list op type-tags))))))6.001, Fall Semester, 1996|Lecture Notes, Octob er 8 { Generic Op erations6Table-Driven Implementation & Installation;;;Ben's (Rectangular) complex implementation...(define (install-rectangular-package);; internal procedures on RepRect = Sch-Num X Sch-Num(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) (atan (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)))));;;Alyssa's (Polar) complex representation...(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


View Full Document

MIT 6 001 - Lecture Notes

Documents in this Course
Quiz 1

Quiz 1

6 pages

Databases

Databases

12 pages

rec20

rec20

2 pages

Quiz II

Quiz II

15 pages

Streams

Streams

5 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?