Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

349 lignes
8.1 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. ;; (define (A m n)
  112. ;; (if (= m 0)
  113. ;; (+ n 1)
  114. ;; (if (= n 0)
  115. ;; (A (- m 1) 1)
  116. ;; (A (- m 1) (A m (- n 1))))))
  117. (assert (= (A 0 0) 1))
  118. (assert (= (A 1 2) 4))
  119. (assert (= (A 3 1) 13))
  120. ;; ;;; ---------------
  121. ;; ;;; Fibonacci
  122. ;; ;;; ---------------
  123. ;; (define (fib n)
  124. ;; (cond ((= n 0) 0)
  125. ;; ((= n 1) 1)
  126. ;; (else (+ (fib (- n 1)) (fib (- n 2))))))
  127. ;; (define (fib2 n)
  128. ;; (fib-iter 1 0 n))
  129. ;; (define (fib-iter a b count)
  130. ;; (if (= count 0)
  131. ;; b
  132. ;; (fib-iter (+ a b) a (- count 1))))
  133. ;; (assert (= (fib 2) 1))
  134. ;; (assert (= (fib 3) 2))
  135. ;; (assert (= (fib 4) 3))
  136. ;; (assert (= (fib 5) 5))
  137. ;; (assert (= (fib 6) 8))
  138. ;; (assert (= (fib2 2) 1))
  139. ;; (assert (= (fib2 3) 2))
  140. ;; (assert (= (fib2 4) 3))
  141. ;; (assert (= (fib2 5) 5))
  142. ;; (assert (= (fib2 6) 8))
  143. ;; ;;; ------------------
  144. ;; ;;; count change
  145. ;; ;;; ------------------
  146. ;; (define (count-change amount)
  147. ;; (define (cc amount kinds-of-coins)
  148. ;; (cond ((= amount 0) 1)
  149. ;; ((or (< amount 0) (= kinds-of-coins 0)) 0)
  150. ;; (else (+ (cc amount (- kinds-of-coins 1))
  151. ;; (cc (- amount (first-denomination kinds-of-coins)) kinds-of-coins)))))
  152. ;; (define (first-denomination kinds-of-coins)
  153. ;; (cond ((= kinds-of-coins 1) 1)
  154. ;; ((= kinds-of-coins 2) 5)
  155. ;; ((= kinds-of-coins 3) 10)
  156. ;; ((= kinds-of-coins 4) 25)
  157. ;; ((= kinds-of-coins 5) 50)))
  158. ;; (cc amount 5))
  159. ;; (assert (= (count-change 100) 292))
  160. ;; ;;; --------------------
  161. ;; ;;; exponentiation
  162. ;; ;;; --------------------
  163. ;; (define (expt b n)
  164. ;; (if (= n 0)
  165. ;; 1
  166. ;; (* b (expt b (- n 1)))))
  167. ;; (define (expt2 b n)
  168. ;; (define (expt-iter b counter product)
  169. ;; (if (= counter 0)
  170. ;; product
  171. ;; (expt-iter b (- counter 1) (* b product))))
  172. ;; (expt-iter b n 1))
  173. ;; (define (fast-expt b n)
  174. ;; (define (even? n)
  175. ;; (= (% n 2) 0))
  176. ;; (cond ((= n 0) 1)
  177. ;; ((even? n) (square (fast-expt b (/ n 2))))
  178. ;; (else (* b (fast-expt b (- n 1))))))
  179. ;; (assert (= (expt 1 2) 1))
  180. ;; (assert (= (expt 2 2) 4))
  181. ;; (assert (= (expt 2 3) 8))
  182. ;; (assert (= (expt2 1 2) 1))
  183. ;; (assert (= (expt2 2 2) 4))
  184. ;; (assert (= (expt2 2 3) 8))
  185. ;; (assert (= (fast-expt 1 2) 1))
  186. ;; (assert (= (fast-expt 2 2) 4))
  187. ;; (assert (= (fast-expt 2 3) 8))
  188. ;; ;;; ----------
  189. ;; ;;; gcd
  190. ;; ;;; ----------
  191. ;; (define (gcd a b)
  192. ;; (if (= b 0)
  193. ;; a
  194. ;; (gcd b (% a b))))
  195. ;; (assert (= (gcd 40 6) 2))
  196. ;; (assert (= (gcd 13 4) 1))
  197. ;; ;;; ----------
  198. ;; ;;; primes
  199. ;; ;;; ----------
  200. ;; (define (smallest-divisor n)
  201. ;; (find-divisor n 2))
  202. ;; (define (find-divisor n test-divisor)
  203. ;; (cond ((> (square test-divisor) n) n)
  204. ;; ((divides? test-divisor n) test-divisor)
  205. ;; (else (find-divisor n (+ test-divisor 1)))))
  206. ;; (define (divides? a b)
  207. ;; (= (% b a) 0))
  208. ;; (define (prime? n)
  209. ;; (= n (smallest-divisor n)))
  210. ;; (assert (prime? 13))
  211. ;; (assert (prime? 11))
  212. ;; (assert (not (prime? 12)))
  213. ;;; ----------------------
  214. ;;; simple integral
  215. ;;; ----------------------
  216. ;; (define (sum term a next b)
  217. ;; (if (> a b)
  218. ;; 0
  219. ;; (+ (term a) (sum term (next a) next b))))
  220. ;; (define (integral f a b dx)
  221. ;; (define (add-dx x) (+ x dx))
  222. ;; (* (sum f (+ a (/ dx 2.0)) add-dx b) dx))
  223. ;; (define (pi-sum a b)
  224. ;; (define (pi-term x) (/ 1.0 (* x (+ x 2))))
  225. ;; (define (pi-next x) (+ x 4))
  226. ;; (sum pi-term a pi-next b))
  227. ;; (assert (< (abs (- (* 8 (pi-sum 1 100)) 3.121595)) 0.0001))
  228. ;; (assert (< (abs (- (integral cube 0 1 0.02) 0.249950)) 0.0001))
  229. ;; ------------------------------------------------------------
  230. ;; F(x,y) = x(1 + xy)^2 + y(1 − y) + (1 + xy)(1 − y)
  231. ;; ------------------------------------------------------------
  232. ;; (define (f x y)
  233. ;; (let ((a (+ 1 (* x y)))
  234. ;; (b (- 1 y)))
  235. ;; (+ (* x (square a))
  236. ;; (* y b)
  237. ;; (* a b))))
  238. ;; (assert (= (f 0 0) 1))
  239. ;; (assert (= (f 1 1) 4))
  240. ;; ;;; ---------------
  241. ;; ;;; find zero
  242. ;; ;;; ---------------
  243. ;; (define (positive? x) (< 0 x))
  244. ;; (define (negative? x) (< x 0))
  245. ;; (define (search f neg-point pos-point)
  246. ;; (let ((midpoint (average neg-point pos-point)))
  247. ;; (if (close-enough? neg-point pos-point)
  248. ;; midpoint
  249. ;; (let ((test-value (f midpoint)))
  250. ;; (cond ((positive? test-value) (search f neg-point midpoint))
  251. ;; ((negative? test-value) (search f midpoint pos-point))
  252. ;; (else midpoint))))))
  253. ;; (define (close-enough? x y) (< (abs (- x y)) 0.001))
  254. ;; (assert (close-enough? (search (lambda (x) (- 1 (square x))) -3 3) -1))