# Structure and Interpretation of Computer Programs* Section 2.4 – 2.5 Presenter: Xiang Zhang 05/04/2010 1 * Harold A., Gerald J. S., Julie S.. Structure.

## Presentation on theme: "Structure and Interpretation of Computer Programs* Section 2.4 – 2.5 Presenter: Xiang Zhang 05/04/2010 1 * Harold A., Gerald J. S., Julie S.. Structure."— Presentation transcript:

Structure and Interpretation of Computer Programs* Section 2.4 – 2.5 Presenter: Xiang Zhang 05/04/2010 1 * Harold A., Gerald J. S., Julie S.. Structure and Interpretation of Computer Programs.

Topics Last Session Data Abstraction Today Multiple Representations for Abstract Data Data-Directed Programming Generic Operations 2

A Running Example – Complex Number System* y x Two Possible Representations for Complex Numbers The rectangular representation: (real, imaginary) The polar representation: (magnitude, angle) One representation can be derived from another * Taken from the book Structure and Interpretation of Computer Programs Real Imaginary r z = x + iy = r e iA A 3

A Running Example – Complex Number System* * Taken from the book Structure and Interpretation of Computer Programs Elementary Operators Addition: z 1 + z 2 Real-part(z 1 + z 2 ) = Real-part(z 1 ) + Real-part(z 2 ) Imaginary-part(z 1 + z 2 ) = Imaginary-part(z 1 ) + Imaginary-part(z 2 ) Subtraction Replace - for + in addition Multiplication Magnitude(z 1 * z 2 ) = Magnitude(z 1 ) * Magnitude (z 2 ) Angle(z 1 * z 2 ) = Angle(z 1 ) + Angle(z 2 ) Division Replace / for * and - for + in multiplication 4

A Running Example – Complex Number System* * Taken from the book Structure and Interpretation of Computer Programs Rectangular Representation Polar Representation add-complex sub-complex mul-complex div-complex Programs that use complex numbers Generic Operators real-part imag-part magnitude angle 5

Dispatching on Type Use Type to Distinguish Representations Example Code for the real-part procedure 3 4 rectangular (rectangular (3 4)) 6

The Weakness of Dispatch on Type Rectangular Polar add-complex sub-complex mul-complex div-complex Programs that use complex numbers real-part imag-part magnitude angle …… X (cases for other representations) 7

Data-directed Programming 8 Rectangular Polar add-complex sub-complex mul-complex div-complex Programs that use complex numbers real-part imag-part magnitude angle …… X Proc Table Query Register OperatorRectangularPolar… real-partreal-part-recreal-part-po… imag-partimag-part-recimag-part-po… mag-partmag-part-recmag-part-po… ang-partang-part-recang-part-po…

A Unified Arithmetic System 9 Rational arithmetic add sub mul div Programs that use rational numbers, complex numbers, and ordinary numbers Rect Polar Complex Arithmetic Ordinary Arithmetic Proc Table Query Register OperatorRationalComplexOrdinary addrat-addcomplex-add+ subrat-subcomplex-sub- mulrat-mulcomplex-mul* divrat-divcomplex-div/

Operate on Different Types - Coercion Supertype and subtype relations An integer is a rational number A rational number is a complex number … 10

11 Rational arithmetic add sub mul div Programs that use rational numbers, complex numbers, and ordinary numbers Rect Polar Complex Arithmetic Ordinary Arithmetic Proc Table Query Register ComplexRationalOrdinary ComplexN/A RationalRat->ComN/A OrdinaryOrd->ComOrd->RatN/A Operate on Different Types - Coercion Coercion Table Query

Summary Dispatch on Type Data-directed Programming Data Coercion 12

Similar presentations