Advertisement
timothy235

sicp-1-1-the-elements-of-programming

Feb 15th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 4.07 KB | None | 0 0
  1. #lang racket
  2.  
  3. ;; 1.1
  4.  
  5. 10
  6. ;; 10
  7. (+ 5 3 4)
  8. ;; 12
  9. (- 9 1)
  10. ;; 8
  11. (/ 6 2)
  12. ;; 3
  13. (+ (* 2 4) (- 4 6))
  14. ;; 6
  15.  
  16. (define a 3)
  17. (define b (+ a 1))
  18. (+ a b (* a b))
  19. ;; 19
  20. (= a b)
  21. ;; #f
  22. (if (and (> b a) (< b (* a b)))
  23.   b
  24.   a)
  25. ;; 4
  26. (cond ((= a 4) 6)
  27.       ((= b 4) (+ 6 7 a))
  28.       (else 25))
  29. ;; 16
  30. (+ 2 (if (> b a) b a))
  31. ;; 6
  32. (* (cond ((> a b) a)
  33.          ((< a b) b)
  34.          (else -1))
  35.    (+ a 1))
  36. ;; 16
  37.  
  38. ;; 1.2
  39.  
  40. (/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 1 5)))))
  41.    (* 3 (- 6 2) (- 2 7)))
  42.  
  43. ;; 1.3
  44.  
  45. (define (sum-of-squares-of-biggest-two-of-three x y z)
  46.   (cond [(and (>= x z) (>= y z)) (+ (* x x) (* y y))]
  47.         [(and (>= x y) (>= z y)) (+ (* x x) (* z z))]
  48.         [else (+ (* y y) (* z z))]))
  49.  
  50. (sum-of-squares-of-biggest-two-of-three 3 4 2)
  51. ;; 25
  52. (sum-of-squares-of-biggest-two-of-three 3 2 4)
  53. ;; 25
  54. (sum-of-squares-of-biggest-two-of-three 2 3 4)
  55. ;; 25
  56.  
  57. ;; 1.4
  58.  
  59. ;; If b > 0, add b to a.  If b <= 0, subtract b from a.
  60.  
  61. (define (a-plus-abs-b a b)
  62.   ((if (> b 0) + -) a b))
  63.  
  64. ;; 1.5
  65.  
  66. ;; Procedure p produces an infinite loop.  If the interpreter is using
  67. ;; applicative-order evaluation, the argument (p) to the function call will always
  68. ;; be evaluated, producing an infinite loop.  If the interpreter is using
  69. ;; normal-order evaluation, then the arguments will only be evaluated as needed by
  70. ;; the if form, and since the predicate is true, the (p) in the alternate clause
  71. ;; will never get evaluated, avoiding the infinite loop.
  72.  
  73. ;; The Racket repl is using applicative-order evaluation.
  74.  
  75. (define (p) (p))
  76. (define (test x y)
  77.   (if (= x 0)
  78.     0
  79.     y))
  80.  
  81. ;; (test 0 (p))
  82. ;; user break
  83.  
  84. ;; 1.6
  85.  
  86. ;; Using the new-if procedure instead of the if special form will produce an
  87. ;; infinte loop because the new-if procedure always evaluates the else-clause,
  88. ;; which calls sqrt-iter again.
  89.  
  90. (define (new-if predicate then-clause else-clause)
  91.   (cond [predicate then-clause]
  92.         [else else-clause]))
  93. (define (average x y)
  94.   (/ (+ x y) 2))
  95. (define (improve guess x)
  96.   (average guess (/ x guess)))
  97. (define (good-enough? guess x)
  98.   (< (abs (- (sqr guess) x)) 0.001))
  99.  
  100. (define (sqrt-iter guess x)
  101.   (if (good-enough? guess x)
  102.     guess
  103.     (sqrt-iter (improve guess x)
  104.                x)))
  105. (define (my-sqrt x)
  106.   (sqrt-iter 1.0 x))
  107.  
  108. (define (sqrt-iter-new-if guess x)
  109.   (new-if (good-enough? guess x)
  110.           guess
  111.           (sqrt-iter-new-if (improve guess x)
  112.                             x)))
  113. (define (my-sqrt-new-if x)
  114.   (sqrt-iter-new-if 1.0 x))
  115.  
  116. (sqrt 2) ; using the racket built-in
  117. ;; 1.4142135623730951
  118. (my-sqrt 2)
  119. ;; 1.4142156862745097
  120. ;; (my-sqrt-new-if 2)
  121. ;; user break
  122.  
  123. ;; 1.7
  124.  
  125. ;; For large numbers my-sqrt can be very inefficient.
  126.  
  127. (time (sqrt 1e13))
  128. ;; cpu time: 0 real time: 0 gc time: 0
  129. ;; 3162277.6601683795
  130. ;; (time (my-sqrt 1e13))
  131. ;; user break
  132.  
  133. ;; For small numbers the margin of error obscures the answer.
  134.  
  135. (sqrt 0.000004)
  136. ;; 0.002
  137. (my-sqrt 0.000004)
  138. ;; 0.03129261341049664 ; this is basically the sqrt of the margin of error
  139. (sqrt 0.001)
  140. ;; 0.03162277660168379
  141.  
  142. (define (new-good-enough? new-guess old-guess)
  143.   (< (abs (/ (- new-guess old-guess)
  144.              old-guess))
  145.      0.001))
  146. (define (new-sqrt-iter old-guess x)
  147.   (let ([new-guess (improve old-guess x)])
  148.     (if (new-good-enough? new-guess old-guess)
  149.       new-guess
  150.       (new-sqrt-iter new-guess x))))
  151. (define (new-my-sqrt x)
  152.   (new-sqrt-iter 1.0 x))
  153.  
  154. ;; For large numbers new-my-sqrt is more efficient but less accurate.
  155.  
  156. (time (new-my-sqrt 1e13))
  157. ;; cpu time: 0 real time: 0 gc time: 0
  158. ;; 3162277.6640104805
  159.  
  160. ;; For small numbers the margin of error no longer hides the answer.
  161.  
  162. (new-my-sqrt 0.000004)
  163. ;; 0.0020000003065983023
  164.  
  165. ;; 1.8
  166.  
  167. (define (cbrt-improve guess x)
  168.   (/ (+ (/ x (sqr guess)) (* 2 guess)) 3))
  169. (define (cbrt-iter old-guess x)
  170.   (let ([new-guess (cbrt-improve old-guess x)])
  171.     (if (new-good-enough? new-guess old-guess)
  172.       new-guess
  173.       (cbrt-iter new-guess x))))
  174. (define (my-cbrt x)
  175.   (cbrt-iter 1.0 x))
  176.  
  177. (my-cbrt 8)
  178. ;; 2.000000000012062
  179. (my-cbrt 27)
  180. ;; 3.0000005410641766
  181. (my-cbrt 1e24)
  182. ;; 100000000.00081353
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement