# public sarabander /p2pu-sicp

### Subversion checkout URL

You can clone with HTTPS or Subversion.

first chapter completed!

commit aa2cdb7e54578472643e6924d4ed1fdfee88708d 1 parent 4e89759
authored
30  1.3/Ex1.35.scm
 ... ... `@@ -0,0 +1,30 @@` 1 `+` 2 `+;; The golden ratio comes from this: ๐ = a/b = (a + b)/a = 1 + b/a = 1 + 1/๐.` 3 `+;; It is already clear from definition that ๐ is a fixed point of x โฆ 1 + 1/x.` 4 `+;; Another way to prove this is to use the exact value of ๐ in x โฆ 1 + 1/x.` 5 `+` 6 `+;; Multiplying both sides of ๐ = 1 + 1/๐ by ๐, we get ๐ยฒ = ๐ + 1.` 7 `+;; Which numerical values satisfy this equation?` 8 `+;; Rearranging, we get ๐ยฒ - ๐ - 1 = 0, where ๐ = (1 ยฑ โ5)/2 โ 1.6180... ` 9 `+` 10 `+;; We will put this value into the right side of x โฆ 1 + 1/x and hope to` 11 `+;; arrive at the left side: 1 + 1/๐ = 1 + 2/(1 ยฑ โ5) = (1 ยฑ โ5 + 2)/(1 ยฑ โ5)` 12 `+;; (3 ยฑ โ5)(1 โ โ5) 3 โ 3โ5 ยฑ โ5 - 5 -2 โ 2โ5 1 ยฑ โ5` 13 `+;; = ---------------- = ---------------- = -------- = ------ = ๐. โ` 14 `+;; (1 ยฑ โ5)(1 โ โ5) 1 - 5 -4 2` 15 `+` 16 `+;; We will now compute the golden ratio using the provided procedure:` 17 `+` 18 `+(define tolerance 0.00001)` 19 `+` 20 `+(define (fixed-point f first-guess)` 21 `+ (define (close-enough? v1 v2)` 22 `+ (< (abs (- v1 v2)) tolerance))` 23 `+ (define (try guess)` 24 `+ (let ((next (f guess)))` 25 `+ (if (close-enough? guess next)` 26 `+ next` 27 `+ (try next))))` 28 `+ (try first-guess))` 29 `+` 30 `+(fixed-point (ฮป (x) (+ 1 (/ x))) 1.0) ; => 1.6180327868852458`
100  1.3/Ex1.36.scm
 ... ... `@@ -0,0 +1,100 @@` 1 `+` 2 `+(define tolerance 0.00001)` 3 `+` 4 `+(define (fixed-point f first-guess)` 5 `+ (define (close-enough? v1 v2)` 6 `+ (< (abs (- v1 v2)) tolerance))` 7 `+ (define (try guess)` 8 `+ (display guess) ;* ` 9 `+ (newline) ;*` 10 `+ (let ((next (f guess)))` 11 `+ (if (close-enough? guess next)` 12 `+ next` 13 `+ (try next))))` 14 `+ (try first-guess))` 15 `+` 16 `+;; Printing ๐ with visible approximation trail` 17 `+(fixed-point (ฮป (x) (+ 1 (/ x))) 1.0) ` 18 `+;; => 1.0` 19 `+;; 2.0` 20 `+;; 1.5` 21 `+;; 1.6666666666666665` 22 `+;; 1.6` 23 `+;; 1.625` 24 `+;; 1.6153846153846154` 25 `+;; 1.619047619047619` 26 `+;; 1.6176470588235294` 27 `+;; 1.6181818181818182` 28 `+;; 1.6179775280898876` 29 `+;; 1.6180555555555556` 30 `+;; 1.6180257510729614` 31 `+;; 1.6180371352785146` 32 `+;; 1.6180327868852458` 33 `+` 34 `+;; And with average damping` 35 `+(fixed-point (ฮป (x) (average x (+ 1 (/ x)))) 1.0)` 36 `+;; => 1.0` 37 `+;; 1.5` 38 `+;; 1.5833333333333333` 39 `+;; 1.6074561403508771` 40 `+;; 1.6147785476652068` 41 `+;; 1.61702925556443` 42 `+;; 1.617723628348796` 43 `+;; 1.6179380934832117` 44 `+;; 1.6180043565683029` 45 `+;; 1.6180248320058461` 46 `+;; 1.6180311591702674` 47 `+` 48 `+;; Solution to xหฃ = 1000 without average damping` 49 `+(fixed-point (ฮป (x) (/ (log 1000) (log x))) 2.0)` 50 `+;; => 2.0` 51 `+;; 9.965784284662087` 52 `+;; 3.004472209841214` 53 `+;; 6.279195757507157` 54 `+;; 3.759850702401539` 55 `+;; 5.215843784925895` 56 `+;; 4.182207192401397` 57 `+;; 4.8277650983445906` 58 `+;; 4.387593384662677` 59 `+;; 4.671250085763899` 60 `+;; 4.481403616895052` 61 `+;; 4.6053657460929` 62 `+;; 4.5230849678718865` 63 `+;; 4.577114682047341` 64 `+;; 4.541382480151454` 65 `+;; 4.564903245230833` 66 `+;; 4.549372679303342` 67 `+;; 4.559606491913287` 68 `+;; 4.552853875788271` 69 `+;; 4.557305529748263` 70 `+;; 4.554369064436181` 71 `+;; 4.556305311532999` 72 `+;; 4.555028263573554` 73 `+;; 4.555870396702851` 74 `+;; 4.555315001192079` 75 `+;; 4.5556812635433275` 76 `+;; 4.555439715736846` 77 `+;; 4.555599009998291` 78 `+;; 4.555493957531389` 79 `+;; 4.555563237292884` 80 `+;; 4.555517548417651` 81 `+;; 4.555547679306398` 82 `+;; 4.555527808516254` 83 `+;; 4.555540912917957` 84 `+;; 4.555532270803653` 85 `+;; ` 86 `+` 87 `+;; And with` 88 `+(fixed-point (ฮป (x) (average x (/ (log 1000) (log x)))) 2.0)` 89 `+;; => 2.0` 90 `+;; 5.9828921423310435` 91 `+;; 4.922168721308343` 92 `+;; 4.628224318195455` 93 `+;; 4.568346513136242` 94 `+;; 4.5577305909237005` 95 `+;; 4.555909809045131` 96 `+;; 4.555599411610624` 97 `+;; 4.5555465521473675` 98 `+;; 4.555537551999825` 99 `+` 100 `+;; Some functions converge significantly faster with average damping.`
37  1.3/Ex1.37.scm
 ... ... `@@ -0,0 +1,37 @@` 1 `+;; a.` 2 `+` 3 `+;; Calculates recursively by descending deeper` 4 `+(define (cont-frac n d k)` 5 `+ (define (recur i)` 6 `+ (if (= i k)` 7 `+ (/ (n i) (d i))` 8 `+ (/ (n i) (+ (d i) (recur (add1 i))))))` 9 `+ (recur 1))` 10 `+` 11 `+(cont-frac (lambda (i) 1.0)` 12 `+ (lambda (i) 1.0)` 13 `+ 5) ; => 0.625` 14 `+` 15 `+(define (range first last)` 16 `+ (cond ((> first last) '())` 17 `+ (else (cons first (range (+ first 1) last)))))` 18 `+` 19 `+(map (ฮป (x) (cont-frac (lambda (i) 1.0)` 20 `+ (lambda (i) 1.0)` 21 `+ x))` 22 `+ (range 1 12))` 23 `+` 24 `+;; To get 4 correct decimal places of 1/๐, which is 0.6180,` 25 `+;; we should use 11-term finite continued fraction.` 26 `+` 27 `+;; b.` 28 `+` 29 `+;; Iterates by climbing up` 30 `+(define (cont-frac n d k)` 31 `+ (define (iter numer denom i)` 32 `+ (if (= i 1)` 33 `+ (/ numer denom)` 34 `+ (iter (n (sub1 i)) ` 35 `+ (+ (d (sub1 i)) (/ numer denom)) ` 36 `+ (sub1 i))))` 37 `+ (iter (n k) (d k) k))`
39  1.3/Ex1.38.scm
 ... ... `@@ -0,0 +1,39 @@` 1 `+;; Depends on 1.37` 2 `+` 3 `+(define numer (ฮป (i) 1.0))` 4 `+` 5 `+(define denom` 6 `+ (ฮป (i) ` 7 `+ (if (zero? (remainder (+ i 1) 3))` 8 `+ (- i (quotient i 3))` 9 `+ 1)))` 10 `+` 11 `+;; Just for checking` 12 `+(map denom (range 1 15)) ; => '(1 2 1 1 4 1 1 6 1 1 8 1 1 10 1)` 13 `+` 14 `+(define (e depth)` 15 `+ (+ 2 (cont-frac numer denom depth)))` 16 `+` 17 `+(e 5)` 18 `+` 19 `+(map e (range 1 15)) ; =>` 20 `+;; '(3.0` 21 `+;; 2.6666666666666665` 22 `+;; 2.75` 23 `+;; 2.7142857142857144` 24 `+;; 2.71875` 25 `+;; 2.717948717948718` 26 `+;; 2.7183098591549295` 27 `+;; 2.718279569892473` 28 `+;; 2.718283582089552` 29 `+;; 2.7182817182817183` 30 `+;; 2.7182818352059925` 31 `+;; 2.7182818229439496` 32 `+;; 2.718281828735696` 33 `+;; 2.7182818284454013` 34 `+;; 2.718281828470584)` 35 `+` 36 `+;; Converges pretty quickly to e` 37 `+` 38 `+;; How much 15-term deep finite cont-frac of e deviates form actual e?` 39 `+(- (exp 1) (e 15)) ; => -1.154e-11`
27  1.3/Ex1.39.scm
 ... ... `@@ -0,0 +1,27 @@` 1 `+;; Depends on 1.37` 2 `+` 3 `+(define (tan-cf x k)` 4 `+ (let ((numer (ฮป (i) ` 5 `+ (if (= i 1)` 6 `+ x` 7 `+ (- (sqr x)))))` 8 `+ (denom (ฮป (i) (- (* 2 i) 1))))` 9 `+ (cont-frac numer denom k)))` 10 `+` 11 `+(tan-cf (/ pi 4) 5) ; => 0.999999986526355` 12 `+(tan-cf (/ pi 4) 9) ; => 1.0 as it should be` 13 `+(tan-cf (/ (* 3 pi) 4) 15) ; => -1.0` 14 `+(tan-cf 0 4) ; => 0` 15 `+(tan-cf pi 15) ; => -1.4135798584282297e-16` 16 `+` 17 `+(map (ฮป (k) (tan-cf (/ pi 4) k)) (range 1 10)) ; =>` 18 `+;; '(0.7853981633974483` 19 `+;; 0.988689239934205` 20 `+;; 0.9997876809149684` 21 `+;; 0.9999978684156948` 22 `+;; 0.999999986526355` 23 `+;; 0.9999999999413254` 24 `+;; 0.9999999999998131` 25 `+;; 0.9999999999999994` 26 `+;; 1.0` 27 `+;; 1.0)`
35  1.3/Ex1.40.scm
 ... ... `@@ -0,0 +1,35 @@` 1 `+` 2 `+;; Definitions from the book` 3 `+(define dx 0.00001)` 4 `+` 5 `+(define (deriv g)` 6 `+ (lambda (x)` 7 `+ (/ (- (g (+ x dx)) (g x))` 8 `+ dx)))` 9 `+` 10 `+(define (cube x) (* x x x))` 11 `+` 12 `+(define (newton-transform g)` 13 `+ (lambda (x)` 14 `+ (- x (/ (g x) ((deriv g) x)))))` 15 `+` 16 `+(define (newtons-method g guess)` 17 `+ (fixed-point (newton-transform g) guess))` 18 `+;; -------------------------------------------` 19 `+` 20 `+(define (cubic a b c)` 21 `+ (ฮป (x)` 22 `+ (+ (cube x)` 23 `+ (* a (sqr x))` 24 `+ (* b x)` 25 `+ c)))` 26 `+` 27 `+(newtons-method (cubic 3 -5 2) 1) ; => -4.278039570812626` 28 `+` 29 `+;; Check` 30 `+((cubic 3 -5 2) -4.278039570812626) ; => 2.4304824819409987e-10` 31 `+` 32 `+(newtons-method (cubic 1 -3 -6) 1) ; => 2.000000000007438` 33 `+` 34 `+;; Check` 35 `+((cubic 1 -3 -6) 2) ; => 0`
22  1.3/Ex1.41.scm
 ... ... `@@ -0,0 +1,22 @@` 1 `+(define (inc x) (add1 x))` 2 `+` 3 `+(define (double f)` 4 `+ (ฮป (x)` 5 `+ (f (f x))))` 6 `+` 7 `+((double inc) 2) ; 4` 8 `+((double sq) 81) ; 3` 9 `+((double sqr) 4) ; 256` 10 `+` 11 `+;; This should return 13:` 12 `+(((double (double double)) inc) 5) ; => 21? ` 13 `+` 14 `+;; What's really happening:` 15 `+` 16 `+;; (double double) = (ฮป (x) (double (double x)))` 17 `+;; (double (double double)) = (ฮป (x) ((double double) ((double double) x))) =` 18 `+;; = (ฮป (x) (double (double (double (double x)))))` 19 `+` 20 `+(((ฮป (x) (double (double (double (double x))))) inc) 5) =` 21 `+((double (double (double (double inc)))) 5) ` 22 `+;; we're incrementing by 16, hence 21`
9  1.3/Ex1.42.scm
 ... ... `@@ -0,0 +1,9 @@` 1 `+(define (compose f g)` 2 `+ (ฮป (x)` 3 `+ (f (g x))))` 4 `+` 5 `+(define (square x) (* x x))` 6 `+` 7 `+((compose square inc) 6) ;=> 49` 8 `+` 9 `+((compose sin sqrt) 7)`
11  1.3/Ex1.43.scm
 ... ... `@@ -0,0 +1,11 @@` 1 `+(define (repeated f times)` 2 `+ (ฮป (x)` 3 `+ (define (again n)` 4 `+ (if (zero? n)` 5 `+ x` 6 `+ (f (again (sub1 n)))))` 7 `+ (again times)))` 8 `+` 9 `+((repeated sqr 2) 5)` 10 `+` 11 `+((repeated add1 5) 10)`
18  1.3/Ex1.44.scm
 ... ... `@@ -0,0 +1,18 @@` 1 `+;; Depends on 1.43` 2 `+` 3 `+(define dx 0.001)` 4 `+` 5 `+(define (smooth f)` 6 `+ (ฮป (x)` 7 `+ (/ (+ (f (- x dx))` 8 `+ (f x)` 9 `+ (f (+ x dx))) ` 10 `+ 3)))` 11 `+` 12 `+(sin (/ pi 6)) ; 0.5` 13 `+` 14 `+((repeated (smooth sin) 8) (/ pi 6)) ; 0.3943356187201898` 15 `+` 16 `+((repeated (smooth sin) 20) (/ pi 6)) ; 0.3081501740128094` 17 `+` 18 `+;; which is logical considering the sinusoid's downward curvature at ฯ/6`
80  1.3/Ex1.45.scm
 ... ... `@@ -0,0 +1,80 @@` 1 `+;; Definitions from the book` 2 `+` 3 `+(define tolerance 0.00001)` 4 `+` 5 `+(define (average x y)` 6 `+ (/ (+ x y) 2))` 7 `+` 8 `+(define (average-damp f)` 9 `+ (lambda (x) (average x (f x))))` 10 `+` 11 `+(define (fixed-point f first-guess)` 12 `+ (define (close-enough? v1 v2)` 13 `+ (< (abs (- v1 v2)) tolerance))` 14 `+ (define (try guess)` 15 `+ (let ((next (f guess)))` 16 `+ (if (close-enough? guess next)` 17 `+ next` 18 `+ (try next))))` 19 `+ (try first-guess))` 20 `+` 21 `+;; From 1.43` 22 `+` 23 `+(define (repeated f times)` 24 `+ (ฮป (x)` 25 `+ (define (again n)` 26 `+ (if (zero? n)` 27 `+ x` 28 `+ (f (again (sub1 n)))))` 29 `+ (again times)))` 30 `+;; ------------------------------` 31 `+` 32 `+(define (cube x) (* x x x))` 33 `+` 34 `+(define (cuberoot x)` 35 `+ (fixed-point (average-damp (ฮป (y) (/ x (sqr y)))) 1.0))` 36 `+` 37 `+(cuberoot 125)` 38 `+` 39 `+(define (4th-root x)` 40 `+ (fixed-point ` 41 `+ ((repeated average-damp 2) (ฮป (y) (/ x (cube y)))) 1.0))` 42 `+` 43 `+(4th-root 16)` 44 `+` 45 `+(define (nth-root x n)` 46 `+ (fixed-point ` 47 `+ ((repeated average-damp 2) (ฮป (y) (/ x (expt y (sub1 n))))) 1.0))` 48 `+` 49 `+(nth-root 81 4)` 50 `+(nth-root 32 5)` 51 `+(nth-root 128 7) ; works until 7th root with double average damping` 52 `+` 53 `+(define (nth-root x n)` 54 `+ (fixed-point ` 55 `+ ((repeated average-damp 3) (ฮป (y) (/ x (expt y (sub1 n))))) 1.0))` 56 `+` 57 `+(nth-root 256 8)` 58 `+(nth-root 512 9)` 59 `+(nth-root 1024 10)` 60 `+(nth-root 32768 15) ; works until 15th root with triple average damping` 61 `+` 62 `+;; Looks like we need at least d average dampings to take ` 63 `+;; at most (2^(d+1) - 1)-th roots` 64 `+` 65 `+;; If we name n = (2^(d+1) - 1) as the order of root, ` 66 `+;; then log(n + 1)` 67 `+;; d = ceiling(---------- - 1)` 68 `+;; log 2` 69 `+` 70 `+(define (nth-root x n)` 71 `+ (let ((d (ceiling (- (/ (log (add1 n))` 72 `+ (log 2))` 73 `+ 1))))` 74 `+ (fixed-point ` 75 `+ ((repeated average-damp d) ` 76 `+ (ฮป (y) (/ x (expt y (sub1 n))))) 1.0)))` 77 `+` 78 `+(nth-root 65536 16)` 79 `+(nth-root (expt 2 1000) 1000)` 80 `+(nth-root 81 1)`
31  1.3/Ex1.46.scm
 ... ... `@@ -0,0 +1,31 @@` 1 `+(define (iterative-improve good? make-better)` 2 `+ (ฮป (y)` 3 `+ (define (iter y)` 4 `+ (if (good? y)` 5 `+ y` 6 `+ (iter (make-better y))))` 7 `+ (iter y)))` 8 `+` 9 `+(define (average a b)` 10 `+ (/ (+ a b) 2))` 11 `+` 12 `+(define (sq-root x)` 13 `+ (let* ((tolerance 0.0001)` 14 `+ (good-enough? (ฮป (guess)` 15 `+ (< (abs (- x (sqr guess))) tolerance)))` 16 `+ (improve (ฮป (guess)` 17 `+ (average guess (/ x guess)))))` 18 `+ ((iterative-improve good-enough? improve) 1.0)))` 19 `+` 20 `+(sq-root 729) ; 27` 21 `+` 22 `+(define (fixed-point f guess)` 23 `+ (let* ((tolerance 0.000001)` 24 `+ (good-enough? (ฮป (guess)` 25 `+ (< (abs (- guess (f guess))) tolerance)))` 26 `+ (improve (ฮป (guess)` 27 `+ (average guess (f guess)))))` 28 `+ ((iterative-improve good-enough? improve) guess)))` 29 `+` 30 `+(fixed-point cos 1.0) ; 0.7391` 31 `+(fixed-point (ฮป (x) (/ 49 x)) 1.0) ; 7`