PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/ch01/1.1/ex-1-1-likerivers12.clj

http://github.com/lisp-korea/sicp
Clojure | 257 lines | 149 code | 62 blank | 46 comment | 24 complexity | 4e563e1ef6b60249e0db97e47b70fd93 MD5 | raw file
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;; p17
  3. (defn square [x]
  4. (* x x))
  5. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  6. ;;; p23
  7. (defn abs [x]
  8. (cond (> x 0) x
  9. (= x 0) 0
  10. (< x 0) (- x)))
  11. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  12. ;;; p24
  13. (defn abs [x]
  14. (cond (< x 0) (- x)
  15. true x))
  16. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  17. ;;; p25
  18. (defn abs [x]
  19. (if (< x 0)
  20. (- x)
  21. x))
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;; p26
  24. (defn >=new [x y]
  25. (or (> x y) (= x y)))
  26. (defn >=new [x y]
  27. (not (< x y)))
  28. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  29. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  30. ;;; p28
  31. ;;; ex-1-3
  32. (defn square [x]
  33. (* x x))
  34. (defn f [a b c]
  35. (cond (> a b)
  36. (+ (square a)
  37. (square (if (> b c)
  38. b
  39. c)))
  40. true
  41. (+ (square b)
  42. (square (if (> a c)
  43. a
  44. c)))))
  45. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  46. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  47. ;;; p28
  48. ;;; ex-1-5
  49. (defn p []
  50. (p))
  51. (defn test1 [x y]
  52. (if (= x 0)
  53. 0
  54. y))
  55. ;;(test1 0 (p))
  56. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  57. ;;; p31
  58. (defn abs [x]
  59. (if (< x 0)
  60. (- x)
  61. x))
  62. (defn improve [guess x]
  63. (average guess (/ x guess)))
  64. (defn average [x y]
  65. (/ (+ x y) 2.0))
  66. (defn good-enough? [guess x]
  67. (< (abs (- (square guess) x)) 0.001))
  68. ;; (defn sqrt-iter [guess x]
  69. ;; (if (good-enough? guess x)
  70. ;; guess
  71. ;; (recur (improve guess x)
  72. ;; x)))
  73. (defn sqrt-iter [guess x]
  74. (if (good-enough? guess x)
  75. guess
  76. (sqrt-iter (improve guess x)
  77. x)))
  78. (defn sqrt-sicp [x]
  79. (sqrt-iter 1.0 x))
  80. (sqrt-sicp 2)
  81. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  82. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  83. ;;; p33
  84. ;;; ex-1-6
  85. (defn square [x]
  86. (* x x))
  87. (defn new-if [predicate then-clause else-clause]
  88. (cond predicate then-clause
  89. true else-clause))
  90. (new-if (= 2 3) 0 5)
  91. (new-if (= 1 1) 0 5)
  92. ;; ok
  93. ;;; works on clojure
  94. (defn sqrt-iter [guess x]
  95. (new-if (good-enough? guess x)
  96. guess
  97. (sqrt-iter (improve guess x)
  98. x)))
  99. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  100. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  101. ;;; p34
  102. ;;; ex-1-7
  103. (defn square [x]
  104. (* x x))
  105. (defn average [x y]
  106. (/ (+ x y) 2))
  107. (defn abs [x]
  108. (if (< x 0)
  109. (- x)
  110. x))
  111. (defn diff [guess x]
  112. (abs (- (square guess) x)))
  113. (defn ratio-of-improve [diff-new diff-old]
  114. (/ diff-new diff-old))
  115. (defn good-enough?-ex-1-7 [guess x diff-old]
  116. (< (ratio-of-improve (diff guess x)
  117. diff-old)
  118. 0.001))
  119. (defn sqrt-iter-ex-1-7 [guess x diff-old]
  120. (if (good-enough?-ex-1-7 guess x diff-old)
  121. guess
  122. (sqrt-iter-ex-1-7 (improve guess x)
  123. x
  124. (diff guess x))))
  125. (defn improve [guess x]
  126. (average guess (/ x guess)))
  127. (defn sqrt-ex-1-7 [x]
  128. (sqrt-iter-ex-1-7 1.0 x x))
  129. (sqrt-ex-1-7 2)
  130. ;;(sqrt-ex-1-7 0.001)
  131. ;;;;;;;;;;;;;;;;;;;;;;;;;;
  132. ;;; just difference between diff-new and diffold
  133. (defn good-enough?-ex-1-7 [guess x diff-old]
  134. (< (abs (- (diff guess x)
  135. diff-old))
  136. 0.001))
  137. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  138. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  139. ;;; p34
  140. (defn cube [x]
  141. (* x x x))
  142. (defn abs [x]
  143. (if (< x 0)
  144. (- x)
  145. x))
  146. (defn square [x]
  147. (* x x))
  148. (defn cube-good-enough? [guess x]
  149. (< (abs (- (cube guess) x)) 0.001))
  150. (defn cube-root-iter [guess x]
  151. (if (cube-good-enough? guess x)
  152. guess
  153. (cube-root-iter (cube-improve guess x) x)))
  154. (defn cube-improve [guess x]
  155. (/ (+ (/ x (square guess)) (* 2 guess)) 3))
  156. (defn cube-root [x]
  157. (cube-root-iter 1.0 x))
  158. (cube-root 8)
  159. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  160. ;;; p39
  161. (defn sqrt-sicp [x]
  162. (letfn [(sqrt-iter [guess x]
  163. (if (good-enough? guess x)
  164. guess
  165. (sqrt-iter (improve guess x)
  166. x)))
  167. (improve [guess x]
  168. (average guess (/ x guess)))
  169. (average [x y]
  170. (/ (+ x y) 2))
  171. (good-enough? [guess x]
  172. (< (abs (- (square guess) x)) 0.001))]
  173. (sqrt-iter 1.0 x)))
  174. (sqrt-sicp 9)
  175. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  176. ;;; p40
  177. (defn square [x]
  178. (* x x))
  179. (defn average [x y]
  180. (/ (+ x y) 2))
  181. (defn abs [x]
  182. (if (< x 0)
  183. (- x)
  184. x))
  185. (defn sqrt-sicp [x]
  186. (letfn [(sqrt-iter [guess]
  187. (if (good-enough? guess)
  188. guess
  189. (sqrt-iter (improve guess))))
  190. (improve [guess]
  191. (average guess (/ x guess)))
  192. (good-enough? [guess]
  193. (< (abs (- (square guess) x)) 0.001))]
  194. (sqrt-iter 1.0)))
  195. (sqrt-sicp 2)