Download presentation

Presentation is loading. Please wait.

Published byGustavo Burnell Modified over 2 years ago

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

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

3
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

4
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

5
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

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

7
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

8
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…

9
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/

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

11
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

12
Summary Dispatch on Type Data-directed Programming Data Coercion 12

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google