SICP Study

2A Higher-Order Procedures

Part 1

Don’t repeat yourself

Now, wherever you see yourself writing the same thing down more than once, there’s something wrong, and you shouldn’t be doing it. And the reason is not because it’s a waste of time to write something down more than once. It’s because there’s some idea here … whenever trying to make complicated systems and understand them, it’s crucial to divide the things up into as many pieces as I can, each of which I understand separately. Sussman

Summation

We can represent sigma notation with a procedure that takes other procedures as arguments:

(define (sum term a next b)
  (if (> a b)
      0
      (+ (term a)
         (sum term (next a) next b))))

Now we can write particular cases easily, without repeating ourselves:

(define (sum-int a b)
  (define (identity x) x)
  (sum identity a 1+ b))
(define (sum-sq a b)
  (sum square a 1+ b))
(define (pi-sum a b)
  (sum (lambda (i) (/ i (* i (+ i 2))))
       a
       (lambda (i) (+ i 4))
       b))

We are separating the things we are adding up from the method of doing the addition. Now, we can change the sum procedure so that it generates an iterative process, and all the specific procedures using sum will benefit.

Part 2

Higher-order procedures

Part 3

Newton’s method

Wishful thinking, essential to good engineering, and certainly essential to good computer science. Sussman

The rights and privileges of first-class citizens