… three foci of phenomena: the human mind, collections of computer programs, and the computer. Every computer program is a model, hatched in the mind, of a real or mental process. These processes, arising from human experience and thought, are huge in number, intricate in detail, and at any time only partially understood. They are modeled to our permanent satisfaction rarely by our computer programs. Thus even though our programs are carefully handcrafted discrete collections of symbols, mosaics of interlocking functions, they continually evolve: we change them as our perception of the model deepens, enlarges, generalizes until the model ultimately attains a metastable place within still another model with which we struggle.

It is better to have 100 functions operate on one data structure than to have 10 functions operate on 10 data structures. As a result the pyramid must stand unchanged for a millennium; the organism must evolve or perish.

The acts of the mind, wherein it exerts its power over simple ideas, are chiefly these three: 1. Combining several simple ideas into one compound one, and thus all complex ideas are made. 2. The second is bringing two ideas, whether simple or complex, together, and setting them by one another so as to take a view of them at once, without uniting them into one, by which it gets all its ideas of relations. 3. The third is separating them from all other ideas that accompany them in their real existence: this is called abstraction, and thus all its general ideas are made. – John Locke, An Essay Concerning Human Understanding (1690)

Well-designed computational systems, like well-designed automobiles or nuclear reactors, are designed in a modular manner, so that the parts can be constructed, replaced, and debugged separately.

Every powerful programming language has three mechanisms for accomplishing this:

  • primitive expressions, which represent the simplest entities the language is concerned with,

  • means of combination, by which compund elements are built from simpler ones, and

  • means of abstraction, by which compound elements can be named and manipulated as units.

In programming, we deal with two kinds pf elements: procedures and data.

Notes: SICP uses Scheme, a dialect of LISP. DrRacket is used to run any code. Expressions are evaluated in recursive way in the context of the environment. How does the interpreter evaluate the expressions? The first simple model is the substitution of a tree. All leaf nodes with primitive operators are evaluated first.

> (define (square x) (* x x))
> (square 3)
9
> (define (sum-of-squares x y)
    (+ (square x) (square y)))
> (sum-of-squares 3 4)
25
> (define (abs x)
    (cond ((> x 0) x)
          ((= x 0) 0)
          ((< x 0) (- x))))
> (abs -9)
9
>
> (define (abs x)
    (if (< x 0) (- x) x))
> (abs -9)
9
> (define (>= x y) (or (> x y) (= x y)))
> (define (>= x y) (not (< x y)))
>
> (define a 3)
> (define b (+ a 1))
> (+ a b (* a b))
19
> (= a b)
#f

In the above code, after cond and if, there are some predicates or one predicate.

# sicp ex-1.3
#
> (define (sum-squares-of-two-larger x y z)
    (define (square x) (* x x))
    (define (square-sum x y)
      (+ (square x) (square y)))
    (cond ((and (< x y) (< x z)) (square-sum y z))
          ((< y z) (square-sum x z))
          (else (square-sum x y))))
      
> (sum-squares-of-two-larger 2 3 4)
25

This solution to ex-1.3 can be done with both square and square-sum procedures defined outside sum-squares-of-two-larger.

Newton’s method to get the square root of a number.

> (define (average x y)
    (/ (+ x y) 2))
> (define (improve guess x)
    (average guess (/ x guess)))
> (define (good-enough? guess x)
    (< (abs (- (square guess) x)) 0.001))
> (define (sqrt-iter guess x)
    (if (good-enough? guess x)
        guess
        (sqrt-iter (improve guess x) x)))
> (define (sqrt x)
    (sqrt-iter 1.0 x))
> (sqrt 9)
3.00009155413138

Tip: in VIM, use ESC key to enter normal mode, then press $ to move the cursor to the end of the line. In the normal mode, use key 0 (zero) to move the cursor to the start of the line.

Ref: