SICP Section 2.1 - 2.2

Apr 1, 2017 20:23 · 486 words · 3 minutes read data abstraction sicp programming

Well, when working with programs, we do not only deal with simple data. We usually deal with more complex data since our programs may be working with complex phenomena. In this chapter, the concept of building abstractions through compound data was introduced. By working with compound data, we can increase the conceptual level and the modularity of our designs and the expressive power of our language. Data abstraction is a technique where the part of a program that deals with how data objects are represented[i.e. its implementation] is isolated from the part of the program that deal with how the data object is used. It allows us to isolate how the data will be used from how it is constructed from simple data.

Scheme provides us with a language construct called cons which enables us to form pairs. It takes in it, 2 arguments and returns a compound data object that contains the 2 arguments as parts. Here’s an example:

(define x (cons a b))

(car x)
;; Returns a

(cdr x)
;; Returns b

You can think of pairs as some glue that can help us come up with more complex data structures. The example explored is that of fractions(rational numbers) which are operated on as a unit. This abstraction of fractions was formed because the ‘fraction’ itself was abstracted from other numbers(numerator and denominator) using the cons as the glue. Here’s an example of rational number arithmetic:

;; Adding one-half to one-third
(add-rat one-half one-third)

Another important concept is abstraction barriers. You can think of them as layers where the item[program] above is separated from the implementation of the items below. This idea makes complex programs easier to maintain and modify. This is because the abstraction layers in themselves reduce complexity in addition to shielding the user from the ‘nuances’ of implementation.

This whole section has been about data, therefore its imperative to discuss what ‘data’ is. The textbook definition of data which is so often given in schools, is that of data being ‘processed information’. There is some truth to this textbook definition; I mean, data is processed information. There is however a more concrete definition of what data is. You can think of data as a set of selectors and constructors which meet a bunch of rules that make them a valid representation of something[say a fraction, or rectangle]. This definition is more concrete. It frees as from thinking of data as stuff like int x in c; we can now think as data as something much more. An example would be data that is defined using procedures. This procedural representation of data will be looked at later, but for now, all I know is that it is called message parsing.

Summary

This was an introduction to what data is and what data abstraction is. Data primitive operations: cons, cdr and car were introduced. The concept of abstraction barriers was also introduced.