Section 2.4 – Multiple representations for Abstract Data

1) Tagged data – Attach-tag simply tags the data, type-tag returns the type of the data, and contents returns the contents of the data, they are defined very similar to pair (cons car cdr). The problem with this approach is you cannot add new data-types easily, and there is clashing of names.

2) In data-directed programming, we install a package for every type of data, for eg rectangular complex numbers, we define procedures to give us the real part, imaginary part, magnitude, and angle. We also have procedures called make-from-real-imag, and make-from-mag-angle, that take in two numbers, cons them (for mag-angle, they compute 1st no. cos 2nd no, and 1st no sin 2no no) and then attach the tag rectangular before them. These procedures are then inserted using get and put in the table.

3) apply-generic is a general procedure, that takes in an operation and arguments. (think of it as taking in a real-part operation and a complex number) It then finds the appropriate procedure from the table that would apply to this kind of data, and if the procedure exists, applies it, else returns an error. Now real-part z is defined as (apply-generic ‘real-part z) and similarly for all other procedures.

4) In tagged data, we had intelligent operations, which acted according to data. We can also have intelligent data, which acts according to operations. This is called message-passing. Here make-from-real-imag x y returns a procedure, that when passed realpart, returns x, when passed imagpart, returns y, and so on. We never actually physically construct something as a complex number (I mean we never construct data, a complex number is now a procedure) Now (apply-generic op arg) is defined as (arg op). This is quite a non-intuitive technique.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: