You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

341 rivejä
7.6 KiB

  1. ;; (define (abs x)
  2. ;; (cond ((< x 0) (- x))
  3. ;; (else x)))
  4. ;; (assert (= (abs 1) 1))
  5. ;; (assert (= (abs (- 2)) 2))
  6. (define (abs x)
  7. (if (< x 0)
  8. (- x)
  9. x))
  10. ;; (assert (= (abs 12) 12))
  11. ;; (assert (= (abs (- 32)) 32))
  12. ;; (define (>= x y)
  13. ;; (or (> x y)
  14. ;; (= x y)))
  15. ;; (assert (>= 2 2))
  16. ;; (assert (>= 3 2))
  17. ;; (assert (not (>= 1 2)))
  18. ;; (assert (not (>= 12 44)))
  19. ;; (define (>= x y)
  20. ;; (not (< x y)))
  21. ;; (assert (>= 2 2))
  22. ;; (assert (>= 3 2))
  23. ;; (assert (not (>= 1 2)))
  24. ;; (assert (not (>= 12 44)))
  25. ;; (define (a-plus-abs-b a b)
  26. ;; ((if (> b 0) + -) a b))
  27. ;; (assert (= (a-plus-abs-b 1 2) 3))
  28. ;; (assert (= (a-plus-abs-b 1 -2) 3))
  29. (define (square x) (* x x))
  30. (define (cube x) (* x x x))
  31. ;; (assert (= ((lambda (x y z)
  32. ;; (+ x y (square z)))
  33. ;; 1 2 3)
  34. ;; 12))
  35. ;; ;;; --------------------
  36. ;; ;;; newtons method
  37. ;; ;;; --------------------
  38. ;; (define tolerance 0.001)
  39. ;; (define (square x)
  40. ;; (* x x))
  41. (define (average x y)
  42. (/ (+ x y) 2))
  43. ;; (define (improve guess x)
  44. ;; (average guess (/ x guess)))
  45. ;; (define (good-enough? guess x)
  46. ;; (< (abs (- (square guess) x)) tolerance))
  47. ;; (define (sqrt-iter guess x)
  48. ;; (if (good-enough? guess x)
  49. ;; guess
  50. ;; (sqrt-iter (improve guess x) x)))
  51. ;; (define (sqrt x)
  52. ;; (sqrt-iter 1.0 x))
  53. ;; (define (sqrt2 x)
  54. ;; (define (good-enough? guess x)
  55. ;; (< (abs (- (square guess) x)) 0.001))
  56. ;; (define (improve guess x)
  57. ;; (average guess (/ x guess)))
  58. ;; (define (sqrt-iter guess x)
  59. ;; (if (good-enough? guess x)
  60. ;; guess
  61. ;; (sqrt-iter (improve guess x) x)))
  62. ;; (sqrt-iter 1.0 x))
  63. ;; (define (sqrt3 x)
  64. ;; (define (good-enough? guess)
  65. ;; (< (abs (- (square guess) x)) 0.001))
  66. ;; (define (improve guess)
  67. ;; (average guess (/ x guess)))
  68. ;; (define (sqrt-iter guess)
  69. ;; (if (good-enough? guess)
  70. ;; guess
  71. ;; (sqrt-iter (improve guess))))
  72. ;; (sqrt-iter 1.0))
  73. ;; (assert (< (abs (- 3 (sqrt 9))) tolerance))
  74. ;; (assert (< (abs (- 4 (sqrt 16))) tolerance))
  75. ;; (assert (not (< (abs (- 4 (sqrt 15))) tolerance)))
  76. ;; (assert (< (abs (- 3 (sqrt2 9))) tolerance))
  77. ;; (assert (< (abs (- 4 (sqrt2 16))) tolerance))
  78. ;; (assert (not (< (abs (- 4 (sqrt2 15))) tolerance)))
  79. ;; (assert (< (abs (- 3 (sqrt3 9))) tolerance))
  80. ;; (assert (< (abs (- 4 (sqrt3 16))) tolerance))
  81. ;; (assert (not (< (abs (- 4 (sqrt3 15))) tolerance)))
  82. ;; ;;; -----------------
  83. ;; ;;; factorial
  84. ;; ;;; -----------------
  85. (define (factorial n)
  86. (if (= n 1)
  87. 1
  88. (* n (factorial (- n 1)))))
  89. (define (factorial2 n)
  90. (fact-iter 1 1 n))
  91. (define (fact-iter product counter max-count)
  92. (if (> counter max-count)
  93. product
  94. (fact-iter (* counter product) (+ counter 1) max-count)))
  95. (define (factorial3 n)
  96. (define (iter product counter)
  97. (if (> counter n)
  98. product
  99. (iter (* counter product) (+ counter 1))))
  100. (iter 1 1))
  101. (assert (= (factorial 6) 720))
  102. (assert (= (factorial2 6) 720))
  103. (assert (= (factorial3 6) 720))
  104. ;;; ----------------
  105. ;;; ackermann
  106. ;;; ----------------
  107. (define (A m n)
  108. (cond ((= m 0) (+ n 1))
  109. ((= n 0) (A (- m 1) 1))
  110. (else (A (- m 1) (A m (- n 1))))))
  111. (assert (= (A 0 0) 1))
  112. (assert (= (A 1 2) 4))
  113. (assert (= (A 3 1) 13))
  114. ;; ;;; ---------------
  115. ;; ;;; Fibonacci
  116. ;; ;;; ---------------
  117. ;; (define (fib n)
  118. ;; (cond ((= n 0) 0)
  119. ;; ((= n 1) 1)
  120. ;; (else (+ (fib (- n 1)) (fib (- n 2))))))
  121. ;; (define (fib2 n)
  122. ;; (fib-iter 1 0 n))
  123. ;; (define (fib-iter a b count)
  124. ;; (if (= count 0)
  125. ;; b
  126. ;; (fib-iter (+ a b) a (- count 1))))
  127. ;; (assert (= (fib 2) 1))
  128. ;; (assert (= (fib 3) 2))
  129. ;; (assert (= (fib 4) 3))
  130. ;; (assert (= (fib 5) 5))
  131. ;; (assert (= (fib 6) 8))
  132. ;; (assert (= (fib2 2) 1))
  133. ;; (assert (= (fib2 3) 2))
  134. ;; (assert (= (fib2 4) 3))
  135. ;; (assert (= (fib2 5) 5))
  136. ;; (assert (= (fib2 6) 8))
  137. ;; ;;; ------------------
  138. ;; ;;; count change
  139. ;; ;;; ------------------
  140. ;; (define (count-change amount)
  141. ;; (define (cc amount kinds-of-coins)
  142. ;; (cond ((= amount 0) 1)
  143. ;; ((or (< amount 0) (= kinds-of-coins 0)) 0)
  144. ;; (else (+ (cc amount (- kinds-of-coins 1))
  145. ;; (cc (- amount (first-denomination kinds-of-coins)) kinds-of-coins)))))
  146. ;; (define (first-denomination kinds-of-coins)
  147. ;; (cond ((= kinds-of-coins 1) 1)
  148. ;; ((= kinds-of-coins 2) 5)
  149. ;; ((= kinds-of-coins 3) 10)
  150. ;; ((= kinds-of-coins 4) 25)
  151. ;; ((= kinds-of-coins 5) 50)))
  152. ;; (cc amount 5))
  153. ;; (assert (= (count-change 100) 292))
  154. ;; ;;; --------------------
  155. ;; ;;; exponentiation
  156. ;; ;;; --------------------
  157. ;; (define (expt b n)
  158. ;; (if (= n 0)
  159. ;; 1
  160. ;; (* b (expt b (- n 1)))))
  161. ;; (define (expt2 b n)
  162. ;; (define (expt-iter b counter product)
  163. ;; (if (= counter 0)
  164. ;; product
  165. ;; (expt-iter b (- counter 1) (* b product))))
  166. ;; (expt-iter b n 1))
  167. ;; (define (fast-expt b n)
  168. ;; (define (even? n)
  169. ;; (= (% n 2) 0))
  170. ;; (cond ((= n 0) 1)
  171. ;; ((even? n) (square (fast-expt b (/ n 2))))
  172. ;; (else (* b (fast-expt b (- n 1))))))
  173. ;; (assert (= (expt 1 2) 1))
  174. ;; (assert (= (expt 2 2) 4))
  175. ;; (assert (= (expt 2 3) 8))
  176. ;; (assert (= (expt2 1 2) 1))
  177. ;; (assert (= (expt2 2 2) 4))
  178. ;; (assert (= (expt2 2 3) 8))
  179. ;; (assert (= (fast-expt 1 2) 1))
  180. ;; (assert (= (fast-expt 2 2) 4))
  181. ;; (assert (= (fast-expt 2 3) 8))
  182. ;; ;;; ----------
  183. ;; ;;; gcd
  184. ;; ;;; ----------
  185. ;; (define (gcd a b)
  186. ;; (if (= b 0)
  187. ;; a
  188. ;; (gcd b (% a b))))
  189. ;; (assert (= (gcd 40 6) 2))
  190. ;; (assert (= (gcd 13 4) 1))
  191. ;; ;;; ----------
  192. ;; ;;; primes
  193. ;; ;;; ----------
  194. ;; (define (smallest-divisor n)
  195. ;; (find-divisor n 2))
  196. ;; (define (find-divisor n test-divisor)
  197. ;; (cond ((> (square test-divisor) n) n)
  198. ;; ((divides? test-divisor n) test-divisor)
  199. ;; (else (find-divisor n (+ test-divisor 1)))))
  200. ;; (define (divides? a b)
  201. ;; (= (% b a) 0))
  202. ;; (define (prime? n)
  203. ;; (= n (smallest-divisor n)))
  204. ;; (assert (prime? 13))
  205. ;; (assert (prime? 11))
  206. ;; (assert (not (prime? 12)))
  207. ;;; ----------------------
  208. ;;; simple integral
  209. ;;; ----------------------
  210. ;; (define (sum term a next b)
  211. ;; (if (> a b)
  212. ;; 0
  213. ;; (+ (term a) (sum term (next a) next b))))
  214. ;; (define (integral f a b dx)
  215. ;; (define (add-dx x) (+ x dx))
  216. ;; (* (sum f (+ a (/ dx 2.0)) add-dx b) dx))
  217. ;; (define (pi-sum a b)
  218. ;; (define (pi-term x) (/ 1.0 (* x (+ x 2))))
  219. ;; (define (pi-next x) (+ x 4))
  220. ;; (sum pi-term a pi-next b))
  221. ;; (assert (< (abs (- (* 8 (pi-sum 1 100)) 3.121595)) 0.0001))
  222. ;; (assert (< (abs (- (integral cube 0 1 0.02) 0.249950)) 0.0001))
  223. ;; ------------------------------------------------------------
  224. ;; F(x,y) = x(1 + xy)^2 + y(1 − y) + (1 + xy)(1 − y)
  225. ;; ------------------------------------------------------------
  226. ;; (define (f x y)
  227. ;; (let ((a (+ 1 (* x y)))
  228. ;; (b (- 1 y)))
  229. ;; (+ (* x (square a))
  230. ;; (* y b)
  231. ;; (* a b))))
  232. ;; (assert (= (f 0 0) 1))
  233. ;; (assert (= (f 1 1) 4))
  234. ;; ;;; ---------------
  235. ;; ;;; find zero
  236. ;; ;;; ---------------
  237. ;; (define (positive? x) (< 0 x))
  238. ;; (define (negative? x) (< x 0))
  239. ;; (define (search f neg-point pos-point)
  240. ;; (let ((midpoint (average neg-point pos-point)))
  241. ;; (if (close-enough? neg-point pos-point)
  242. ;; midpoint
  243. ;; (let ((test-value (f midpoint)))
  244. ;; (cond ((positive? test-value) (search f neg-point midpoint))
  245. ;; ((negative? test-value) (search f midpoint pos-point))
  246. ;; (else midpoint))))))
  247. ;; (define (close-enough? x y) (< (abs (- x y)) 0.001))
  248. ;; (assert (close-enough? (search (lambda (x) (- 1 (square x))) -3 3) -1))