sicp/notes.txt

87 lines
1.6 KiB
Plaintext

1.9:
(define (+ a b)
(if (= a 0) b (inc (+ (dec a) b))))
(+ 4 5)
(if (= 4 0) 4 (inc (+ (dec 4) 5)))
(inc (+ 3 5))
(inc (inc (+ 2 5)))
(inc (inc (inc (+ 1 5))))
(inc (inc (inc (inc (+ 0 5)))))
(inc (inc (inc (inc 5))))
(inc (inc (inc 6)))
(inc (inc 7))
(inc 8)
9
Recursive
(define (+ a b)
(if (= a 0) b (+ (dec a) (inc b))))
(+ 4 5)
(+ 3 6)
(+ 2 7)
(+ 1 8)
(+ 0 9)
9
Iterative
1.10:
(define (A x y)
(cond ((= y 0) 0)
((= x 0) (+ 2 y))
((= y 1) 2)
(else (A (- x 1)
(A x (- y 1))))))
(A 1 10)
(A 0 (A 1 9))
(A 0 (A 0 (A 1 8)))
(A 0 (A 0 (A 0 (A 1 7))))
(A 0 (A 0 (A 0 (A 0 (A 1 6)))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 1 5))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 4)))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 3)))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 2))))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 1)))))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 2))))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 4))))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 6)))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 8)))))))
(A 0 (A 0 (A 0 (A 0 (A 0 10))))))
(A 0 (A 0 (A 0 (A 0 12)))))
(A 0 (A 0 (A 0 14))))
(A 0 (A 0 16)))
(A 0 18)
22
(A 2 4) = 16
(A 3 3) = 16
1.34
(define (f g) (g 2))
(f f) gives error Error: call of non-procedure: 2. This is because it is expecting the
second argument to be a procedure (implicitly from the fact that it applies it to 2).
1.35
Golden ratio: phi = (1 + sqrt(5))/2
Let x' be a fixed point of x |-> 1 + 1/x. Then
x' = 1 + 1/x' = (x' + 1)/x'
x'^2 = x' + 1
x'^2 - x' - 1 = 0
Solutions: (1 +- sqrt(1 + 4)) / 2. So phi is one of the fixed points.