SICP Study

2B Compound Data

Part 1

Recap

In the beginning, we learned about

Then, we learned how to use higher-order procedures to represent general methods of computation. This gave us extraordinary expressive power.

Layered system

So the crucial idea is that when we’re building things, we divorce the task of building things from the task of implementing the parts. And in a large system, of course, we have abstraction barriers like this at lots, and lots, and lots of levels. Abelson

Rational number arithmetic

(define (+rat x y)
  (make-rat
   (+ (* (numer x) (denom y))
      (* (numer y) (denom x)))
   (* (denom x) (denom y))))

Why do we need compound data?

Part 2

Pairs

Lowest terms

(define (make-rat n d)
  (let ((g (gcd n d)))
    (cons (/ n g) (/ d g))))

Abstraction layer

Why use data abstraction?

Designing systems

Highlights

See, in general, as systems designers, you’re forced with the necessity to make decisions about how you’re going to do things, and in general, the way you’d like to retain flexibility is to never make up your mind about anything until you’re forced to do it. The problem is, there’s a very, very narrow line between deferring decisions and outright procrastination. So you’d like to make progress, but also at the same time, never be bound by the consequences of your decisions. Abelson

Highlights

I said that computer science is a lot like magic, and it’s sort of good that it’s like magic. There’s a bad part of computer science that’s a lot like religion. And in general, I think people who really believe that you design everything before you implement it basically are people who haven’t designed very many things.

The real power is that you can pretend that you’ve made the decision and then later on figure out which one is right, which decision you ought to have made. And when you can do that, you have the best of both worlds. Abelson

Part 3

Data abstraction

Points and line segments

Part 4

Abstract data and contracts

Implementation of pairs

(define (cons a b)
  (lambda (pick)
    (cond ((= pick 1) a)
          ((= pick 2) b))))
(define (car x) (x 1))
(define (cdr x) (x 2))

Conclusion