notes | journal | about | archive | sourcehut | github

December 16, 2018

I decided to start the legendary Structure and Interpretation of Computer Programs, after a brief foray into Emacs Lisp. This is one of those books that's challenging enough to make you feel like a toddler again. I love it.

My goal is to work though most of Chapter 1 by the end of winter break, and finish the book before I graduate.

I'm including some of the code answers I got below. The book can be found here, and I referred to Scheme wiki's SICP solutions for certain problems (like 1.11 and 1.12).

1.3

   (define (test a b c)
     (+ (square
         (cond ((and (> a b) (> a c)) a)
                    ((and (> b a) (> b c)) b)
                         (else c)))

   (square
         (cond ((exclusiveor (> a b) (> a c)) a)
                    ((exclusiveor (> b a) (> b c)) b)
                    (else c)))))

   (define (exclusiveor a b)
     (and (not (and a b)) (not (and (not a) (not b)))))
   (define (square a) (* a a))

   (test 1 2 3)

1.8 combined with 1.7's good-enough? test

  (define (cube-root x)
    (cube-iter x 1.0))

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

  (define (cube-iter x guess)
    (if (good-enough? guess (improve guess x))
        guess
        (cube-iter x (improve guess x))))

  (define (improve guess x)
    (/ (+ (* 2 guess) (/ x(square guess)))
       3))
  (define (good-enough? guess newguess)
    (< (abs (- guess newguess)) (* .001 guess)))

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

  (cube-root 8)

1.11:

  ; recursive (much easier)
  ;; (define (f n)
  ;;   (cond ((< n 3) n)
  ;;         (else (+
  ;;                (f (- n 1))
  ;;                (* 2 (f (- n 2)))
  ;;                (* 3 (f (- n 3)))))))

  ;;iterative, harder, doesn't help that the pattern isn't
  ;;predictable. should eval to 0, 1, 2, 4, 11, 25, 59, and 142.

  (define (f n)
    (cond ((< n 3) n)
          (else 
    (f-iter 2 1 0 n))))

  (define (f-iter a b c number)
    (cond ((= number 3) (+ a (* 2 b) (* 3 c)))
          (else (f-iter (+ a (* 2 b) (* 3 c) ) a b (- number 1)))
          ))

1.12

  ;; after mucking about with strings and realizing I have no idea how
  ;; this is done in MIT Scheme, I decided not to print a pretty
  ;; triangle. This code will compute the number in row r and "column" c
  (define (computepascal r c)
    (cond ((or (= r c) (= c 1)) 1)
          (else (+ (computepascal (- r 1) (- c 1)) (computepascal (- r 1) c)))))

  (computepascal 5 3) ;correct number is 6

1.15

  (define (cube x) (* x x x))
  (define (p x) (- (* 3 x) (* 4 (cube x))))
  (define (sine angle)
     (if (not (> (abs angle) 0.1))
         angle
         (p (sine (/ angle 3.0)))))

  ; a gets the answer to 1.15a
  (define (a p)
    (a-helper p 0))

    (define (a-helper p count)
    (cond
      ((not (> p 0.1)) count)
      (else (a-helper (/ p 3) (+ count 1)))))


  (a 12.15)

1.16

  ;;fast-exp with iterative instead of recursive

  (define (fast-exponent x n)
    (cond ((not (= (remainder n 2) 0)) (* x (fast-exponent x (- n 1))))
          (else (fast-exponent-helper x n))))

  (define (fast-exponent-helper x n)
    (cond ((= n 2) (* x x))
          (else (fast-exponent (square x) (/ n 2)))))

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

I'll need to return to 1.13, which required some math skills relating to proofs that I haven't used in ages. Thankfully Bill the Lizard posted a walkthrough.

A few other exercises didn't require much besides understanding the question, or a "yes" or "no" answer, (e.g. 1.4). Some others were best left in the REPL (1.1), or I did them by hand (1.14).

Reading

This week I read:

The Lambda Calculus for Absolute Dummies (like myself)

Technology is killing the myth of human centrality – let's embrace our demotion

How to introduce yourself so you'll be unforgettable (in a good way!)

The commas that cost companies millions

How to Recover from Romantic Heartbreak

A PhD should be about improving society, not chasing academic kudos

You Cannot Serve Two Masters: The Harms of Dual Affiliation

Elon Musk says humans must become cyborgs to stay relevant. Is he right?

Maybe They're Just Bad People

A national expert in human remains, Ann Ross used cutting-edge science to help solve one of Raleigh's most notorious murders

The Case for Getting Rid of Borders—Completely

Trying to arrive at clarity on the state of higher education

Coming to grips with a frustrating truth

That last one is pretty interesting.