Advertisement
timothy235

sicp-1-3-4-procedures-as-returned-values

Feb 17th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 4.52 KB | None | 0 0
  1. #lang racket
  2.  
  3. ;;;;;;;;;;
  4. ;; 1.40 ;;
  5. ;;;;;;;;;;
  6.  
  7. (define tolerance 0.00001)
  8.  
  9. (define (fixed-point f first-guess)
  10.   (define (close-enough? v1 v2)
  11.     (< (abs (- v1 v2)) tolerance))
  12.   (define (try guess)
  13.     (let ([next (f guess)])
  14.       (if (close-enough? guess next)
  15.         next
  16.         (try next))))
  17.   (try first-guess))
  18.  
  19. (define dx 0.00001)
  20.  
  21. (define (deriv g)
  22.   (lambda (x)
  23.     (/ (- (g (+ x dx)) (g x))
  24.        dx)))
  25.  
  26. (define (newton-transform g)
  27.   (lambda (x)
  28.     (- x (/ (g x) ((deriv g) x)))))
  29.  
  30. (define (newtons-method g guess)
  31.   (fixed-point (newton-transform g) guess))
  32.  
  33. (define (cubic a b c)
  34.   (lambda (x)
  35.     (+ (* x x x)
  36.        (* a x x)
  37.        (* b x)
  38.        c)))
  39.  
  40. (newtons-method (cubic 0 0 -8) 1.0)
  41. ;; 2.000000000036784
  42.  
  43. (newtons-method (cubic 1 1 1) 1.0)
  44. ;; -0.9999999999997796
  45.  
  46. ;;;;;;;;;;
  47. ;; 1.41 ;;
  48. ;;;;;;;;;;
  49.  
  50. (define (double f)
  51.   (lambda (x) (f (f x))))
  52.  
  53. (((double (double double)) add1) 5)
  54. ;; 21
  55.  
  56. ;;;;;;;;;;
  57. ;; 1.42 ;;
  58. ;;;;;;;;;;
  59.  
  60. (define (my-compose f g)
  61.   (lambda (x) (f (g x))))
  62.  
  63. ((my-compose sqr add1) 6)
  64. ;; 49
  65.  
  66. ;;;;;;;;;;
  67. ;; 1.43 ;;
  68. ;;;;;;;;;;
  69.  
  70. (define (repeated f n)
  71.   (if (= n 1)
  72.     f
  73.     (my-compose f (repeated f (sub1 n)))))
  74.  
  75. ((repeated sqr 2) 5)
  76. ;; 625
  77.  
  78. ;;;;;;;;;;
  79. ;; 1.44 ;;
  80. ;;;;;;;;;;
  81.  
  82. (define (smooth f)
  83.   (lambda (x)
  84.     (/ (+ (f (- x dx))
  85.           (f x)
  86.           (f (+ x dx)))
  87.        3)))
  88.  
  89. (define (n-fold-smooth f n)
  90.   ((repeated smooth n) f))
  91.  
  92. (for ([i (in-range 1 6)])
  93.   (displayln (log i)))
  94. ;; 0
  95. ;; 0.6931471805599453
  96. ;; 1.0986122886681098
  97. ;; 1.3862943611198906
  98. ;; 1.6094379124341003
  99.  
  100. (for ([i (in-range 1 6)])
  101.   (displayln (((repeated smooth 10) log) i)))
  102. ;; -3.333332199762096e-010
  103. ;; 0.693147180476612
  104. ;; 1.0986122886310727
  105. ;; 1.386294361099058
  106. ;; 1.6094379124207672
  107.  
  108. ;;;;;;;;;;
  109. ;; 1.45 ;;
  110. ;;;;;;;;;;
  111.  
  112. (define (average a b)
  113.   (/ (+ a b)
  114.      2))
  115.  
  116. (define (average-damp f)
  117.   (lambda (x) (average x (f x))))
  118.  
  119. (define (get-damped-root power times)
  120.   (lambda (x)
  121.     ; find the power-th root of x using fixed-point
  122.     ; and damping the indicated number of times
  123.     (fixed-point
  124.       ((repeated average-damp times) (lambda (y) (/ x (expt y (sub1 power)))))
  125.       1.0)))
  126.  
  127. (define (investigate power min-times)
  128.   (for ([times (in-range min-times (add1 power))])
  129.     (printf "power is ~a, times is ~a, answer is ~a ~n"
  130.             power
  131.             times
  132.             ((get-damped-root power times) (expt 2 power)))))
  133.  
  134. ;; I used the investigate function to find all powers and times,
  135. ;; for powers = 2, 3, ..., 17, and times = 1, 2, ..., power,
  136. ;; that did not converge.
  137.  
  138. ;; did not converge
  139. ;; power   times-damped
  140. ;; 4       1
  141. ;; 5       1
  142. ;; 8       2
  143. ;; 9       2
  144. ;; 10      2
  145. ;; 11      2
  146. ;; 13      1
  147. ;; 13      2
  148. ;; 14      2
  149. ;; 16      3
  150. ;; 17      2
  151. ;; 17      3
  152.  
  153. ;; I don't see any obvious pattern here.  The sample is too small.  But damping
  154. ;; four times should work for most small powers.
  155.  
  156. (define (my-root n)
  157.   (get-damped-root n 4))
  158.  
  159. (for ([n (in-range 2 20)])
  160.   (displayln ((my-root n) (expt 2 n))))
  161. ;; 1.9999378937004895
  162. ;; 1.9999583385968283
  163. ;; 1.9999751575244828
  164. ;; 2.0000200551235574
  165. ;; 2.000011071925238
  166. ;; 2.0000106805408264
  167. ;; 2.000008820504543
  168. ;; 2.0000074709755573
  169. ;; 2.0000044972691784
  170. ;; 2.0000038760178884
  171. ;; 2.000001241323132
  172. ;; 2.000001923509733
  173. ;; 2.0000006317785997
  174. ;; 2.0000001141598975
  175. ;; 2.000000000076957
  176. ;; 2.0000000561635765
  177. ;; 2.0000005848426476
  178. ;; 2.0000003649180282
  179.  
  180. ;;;;;;;;;;
  181. ;; 1.46 ;;
  182. ;;;;;;;;;;
  183.  
  184. (define (iterative-improve good-enough? improve-guess)
  185.   (define (iterate guess)
  186.     (if (good-enough? guess)
  187.       guess
  188.       (iterate (improve-guess guess))))
  189.   iterate)
  190.  
  191. (define (new-sqrt x)
  192.   (define (good-enough? guess)
  193.     (< (abs (- (sqr guess) x)) 0.001))
  194.   (define (improve-guess guess)
  195.     (average guess (/ x guess)))
  196.   ((iterative-improve good-enough? improve-guess) 1.0))
  197.  
  198. (for ([i (in-range 2 10)])
  199.   (displayln (new-sqrt i)))
  200. ;; 1.4142156862745097
  201. ;; 1.7321428571428572
  202. ;; 2.0000000929222947
  203. ;; 2.2360688956433634
  204. ;; 2.4494943716069653
  205. ;; 2.64576704419029
  206. ;; 2.8284685718801468
  207. ;; 3.00009155413138
  208.  
  209. (define (new-fixed-point f first-guess)
  210.   (define (good-enough? guess)
  211.     (< (abs (- guess (f guess))) 0.00001))
  212.   ((iterative-improve good-enough? f) first-guess))
  213.  
  214. ;; solve x ^ x = 1000 by finding the fixed point of f(x) = log(1000) / log(x)
  215. (define soln (new-fixed-point (lambda (x) (/ (log 1000) (log x)))
  216.                               2.0))
  217. soln
  218. ;; 4.555540912917957
  219. (expt soln soln)
  220. ;; 1000.0131045064136
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement