# SICP with Python / chap1.py

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 #!/usr/local/bin/python # -*- coding: utf-8 -*- print(''' Exercise 1.5 ============''') def p(): p() def test(x, y): if x == 0: return 0 else: return y print(test(0, p)) print(''' 1.1.7 Example: Square Roots by Newton's Method ==============================================''') def average(x, y): return (x + y) / 2 def improve(guess, x): return average(guess, x / guess) def square(x): return x * x def good_enough(guess, x): return abs(square(guess) - x) < 0.001 def sqrt_iter(guess, x): if good_enough(guess, x): return guess else: return sqrt_iter(improve(guess, x), x) def sqrt(x): return sqrt_iter(1.0, x) def _test(): print(sqrt(9)) print(sqrt(100 + 37)) print(sqrt(sqrt(2) + sqrt(3))) print(square(sqrt(1000))) _test() print(''' Exercise 1.7 ============''') print(''' good_enough(10**10, 10**10 + 1) expect: True, actual: {0}'''.format(good_enough(10**10, 10**10 + 1))) print(''' good_enough(0.00001, 0.0001) expect: False, actual: {0}'''.format(good_enough(0.0001, 0.0009))) print('re-define "good_enough" to show calculation process') def good_enough(guess, x): print('guess: {0}'.format(guess)) return abs(square(guess) - x) < 0.001 print(''' sqrt(10**10) (obviously is 10**5) ---------------------------------''') print('actual: {0}'.format(sqrt(10**10))) print(''' sqrt(0.0001) (obviously is 0.01) ----------------------------------''') print('actual: {0}'.format(sqrt(0.0001))) print(''' refining good_enough function... def good_enough(guess, x): print('guess: {0}'.format(guess)) return abs(square(guess) / x - 1) < 0.001''') print('re-run') def good_enough(guess, x): print('guess2: {0}'.format(guess)) return abs((square(guess) / x) - 1) < 0.001 print(''' sqrt(10**10) (obviously is 10**5) ---------------------------------''') print('actual: {0}'.format(sqrt(10**10))) print(''' sqrt(0.0001) (obviously is 0.01) ----------------------------------''') print('actual: {0}'.format(sqrt(0.0001))) print(''' Exercise 1.8''') def improve(guess, x): return (x / guess**2 + 2 * guess) / 3 def qube(x): return x**3 def good_enough(guess, x): return abs(qube(guess) / x - 1) < 0.001 def qbrt_iter(guess, x): if good_enough(guess, x): return guess else: return qbrt_iter(improve(guess, x), x) def qbrt(x): return qbrt_iter(1.0, x) def _qbrt_test(): print(qbrt(27)) print(qbrt(1000)) print(qbrt(2)) print(qbrt(0.1)) _qbrt_test() print(''' 1.1.8 Procedures as Black-Box Abstractions =========================================== ''') print(''' Internal definitions and block structure ----------------------------------------''') def sqrt(x): def good_enough(guess, x): return abs(square(guess) - x) < 0.001 def improve(guess, x): return average(guess, x / guess) def sqrt_iter(guess, x): if good_enough(guess, x): return guess else: return sqrt_iter(improve(guess, x), x) return sqrt_iter(1.0, x) _test() print(''' lexical scoping''') def sqrt(x): def good_enough(guess): return abs(square(guess) - x) < 0.001 def improve(guess): return average(guess, x / guess) def sqrt_iter(guess): if good_enough(guess): return guess else: return sqrt_iter(improve(guess)) return sqrt_iter(1.0) _test() print(''' Exercise 1.10 =============''') def A(x, y): if y == 0: return 0 elif x == 0: return 2 * y elif y == 1: return 2 else: return A(x - 1, A(x, y - 1)) print(A(1, 10)) print(A(2, 4)) print(A(3, 3))