SICP Study

1A Overview and Introduction to Lisp

Part 1

Computer science

Declarative vs. imperative

Mathematical declarative statement (what-is knowledge): The square root of xx is the yy such that y0y ≥ 0 and y2=x.y^2 = x\htmlClass{math-punctuation}{\text{.}}

Imperative instructions (how-to knowledge): approximate the square root of xx with the following steps:

  1. Make a guess, G.G\htmlClass{math-punctuation}{\text{.}}
  2. Improve the guess by averaging GG and x/G.x/G\htmlClass{math-punctuation}{\text{.}}
  3. Keep improving until the guess is good enough.

Processes and Lisp

Complexity and computer science

Highlights

The real problems come when we try to build very, very large systems … nobody can really hold them in their heads all at once … the only reason that that’s possible is because there are techniques for controlling the complexity of these large systems. And these techniques that are controlling complexity are what this course is really about. And in some sense, that’s really what computer science is about. Abelson

Highlights

So in that sense, computer science is like an abstract form of engineering. It’s the kind of engineering where you ignore the constraints that are imposed by reality. Abelson

Techniques for managing complexity

Black box abstraction: Take something and build a box around it. The important thing is that you don’t care what is going on inside the box—it’s not important. Black-box abstraction suppresses detail. This allows you to go on and build bigger boxes.

Conventional interfaces: Agreed upon ways of connecting things together. Like standard impedances in electrical engineering.

Metalinguistic abstraction: Another way of controlling complexity is to choose a new design language (a domain-specific language, or DSL) that will highlight different aspects of the system. It will emphasize some kinds of details and suppress others. This is the technology for building new computer languages.

The process of interpreting Lisp in Lisp is like a giant wheel of two processes, apply and eval, which reduce expressions to each other. Very magical.

Part 2

Three main features

Primitive elements: Here are some primitive elements is Lisp: 3, 14.4, 5, +. These are all names that represents things. The first three represent numbers, and the last one represents the concept of addition.

Means of combination:

Means of abstraction: This is accomplished in Lisp with define. Defining something gives a name to an expression. We write this the same way as a regular combination, but define is not a procedure—it is a special form. We can also define procedures this way:

(define (square x) (* x x))

We can also make it more clear that we are naming something:

(define square (lambda (x) (* x x)))

The former notation is just syntactic sugar for the latter: a more convenient surface forms for typing something. The former desugars to the latter.

In Lisp, you do not make arbitrary distinctions between things that are defined in the language and things that happen to be built-in.

Case analysis

We do case analysis in Lisp using cond.

(define (abs x)
  (cond ((< x 0) (- x))
        ((= x 0) 0)
        ((> x 0) x)))

Each line is a clause consisting of a predicate (true or false) and an action. We can use if if there is a single case:

(define (abs x)
  (if (< x 0)
      (- x)
      x))

You can think of if as syntactic sugar for cond or vice versa.

Recursion

Block structure