## Posted By

darkbaron1912 on 08/05/10

# thi lap trinh ham

/ Published in: Scheme

1. (define (pitagore a b)
2. (let ((a2 (* a a)) (b2 (* b b)))
3. (sqrt (+ a2 b2))))
4.
5. (define (divisibleBy? number divisor)
6. (= (remainder number divisor) 0))
7. ; kiem tra 1 nam co nhua hay ko
8. (define (isBissextile? year)
9. (or (divisibleBy? year 400)
10. (and (divisibleBy? year 4)
11. (not (divisibleBy? year 100)))))
12.
13. ; tinh giai thua
14. (define (fac n)
15. (if (zero? n) ; hoặc (<= n 0) để đảm bảo xử lý mọi số n
16. 1
17. (* n (fac (- n 1)))))
18. ; ham fibonaci
19. (define (fib n)
20. (if (<= n 1)
21. n
22. (+ (fib (- n 1)) (fib (- n 2)))))
23.
24. #lang scheme
25. ;Cau 1
26. (define (i-fac n M) (
27. if (zero? n) M
28. (i-fac (- n 1) (* M n))
29. ))
30. (define (facz n)(i-fac n 1))
31. ;Cau 2
32. (define (fib n) (
33. if (<= n 1) n
34. (+ (fib (- n 1)) (fib (- n 2)))
35. ))
36. (define (i-fib n a b) (
37. if (<= n 1) a
38. (i-fib (- n 1) (+ a b) a)
39. ))
40. (define (fibz n)(
41. i-fib n 1 0
42. ))
43. ;Cau 3
44. (define (sum_inv n)(
45. if (<= n 0) 0
46. (+ (/ 1.0 n) (sum_inv (- n 1)))
47. ))
48. (define (i-sum_inv n p) (
49. if (<= n 0) p
50. (i-sum_inv (- n 1) (+ p (/ 1.0 n)))
51. ))
52. (define (sum_invz n)(i-sum_inv n 0))
53. ;Cau 3
54. (define (x^n x n)(
55. if (<= n 0) 1
56. (* x (x^n x (- n 1)))
57. ))
58. (define (i-x^n x n P)(
59. if (<= n 0) P
60. (i-x^n x (- n 1) (* P x))
61. ))
62. (define (x^nz x n) (i-x^n x n 1))
63. ;Cau 4
64. (define (tohop n k)(
65. cond ((= k 0) 1)
66. ((= k n) 1)
67. (else (+ (tohop (- n 1) k) (tohop (- n 1) (- k 1))))
68. ))
69. ;Cau 5
70. (define (PIover4 n)(
71. if (<= n 0) 1
72. ;2*n+1
73. (+ (/ ( if (= (remainder n 2) 1) -1.0 1.0) (+ (* 2 n) 1)) (PIover4 (- n 1)))
74. ))
75. (define (PI) (* 4(PIover4 100000)))
76. ;Cau 6
77. (define (tongbp n) (
78. if (<= n 0) 0
79. (+ (* n n) (tongbp (- n 1)))
80. ))
81. (define (i-tongbp n S) (
82. if (<= n 0) S
83. ( i-tongbp (- n 1) (+ (* n n) S))
84. ))
85. (define (tongbpz n) (i-tongbp n 0))
86. ;Cau 7
87. (define (bai7 x y)(
88. / (+ (sqrt (+ (* x x) (* y y))) (sqrt (- (* x x) (* y y)))) (+ 1 (sqrt (+ (* x x) (* y y))) (sqrt (- (* x x) (* y y))))
89. ))
90. (define (bai7z x y )(
91. let (
92. (t1 (sqrt (+ (* x x) (* y y))))
93. (t2 (sqrt (- (* x x) (* y y))))
94. )(/ (+ t1 t2) (+ 1 t1 t2))
95. ))
96. ;cau 8
97. (define (nbsec h m s)(
98. + (* h 3600) (* m 60) s
99. ))
100. ;cau 9
101. (define (i-bai9 x S n t e)(
102. if (< (abs t) e) S
103. (i-bai9 x (+ S (* 1.0 t)) (+ n 1) (* t (/ (* (- (* 2 n) 2) x x) (* (- (* 2 n) 1) 2 n))) e)
104. ))
105. (define (bai9 x)(i-bai9 x 1 2 (/ (* x x) 2) 0.000001))
106. ;cau 10
107. (define (legendre x n) (
108. cond ((= n 0) 1)
109. ((= n 1) x)
110. (else (
111. - ( * 1.0 x (legendre x (- n 1))) (/ (* (+ n 1) (+ n 1) (legendre x (- n 2))) (* (+ (* 2 n) 1) (+ (* 2 n) 3)))
112. ))
113. ))
114.
115.
116.
117. (define (dinhthuc a11 a12 a13 a21 a22 a23 a31 a32 a33)
118. (- (+ (* a11 a22 a33) (* a12 a23 a31) (* a13 a21 a32))
119. (+ (* a13 a22 a31) (* a11 a32 a23) (* a21 a33 a12))))
120. (define (hpt3an a1 b1 c1 d1 a2 b2 c2 d2 a3 b3 c3 d3)
121. (let
122. ((d (dinhthuc a1 b1 c1 a2 b2 c2 a3 b3 c3))
123. (dx (dinhthuc b1 c1 d1 b2 c2 d2 b3 c3 d3))
124. (dy (dinhthuc a1 d1 c1 a2 d2 c2 a3 d3 c3))
125. (dz (dinhthuc a1 b1 d1 a2 b2 d2 a3 b3 d3)))
126. (if (zero? d)
127. (if (and (zero? dx) (zero? dy) (zero? dz))
128. (display "He phuong trinh co vo so nghiem")
129. (display "He phuong trinh vo nghiem"))
130. (begin
131. (display "He phuong trinh co nghiem") (newline)
132. (display "x=") (display (/ dx d)) (newline)
133. (display "y=") (display (/ dy d)) (newline)
134. (display "z=") (display (/ dz d))
135. )
136. )
137. )
138. )
139.
140.
141. (define (bai7a)
142. (begin
143. (display "nhap 1 so <0 >1000")
145. (if ( or (< mynum 0 ) (> mynum 1000))
146. (bai7a)
147. (begin
148. (display " ket qua binh phuong la ")
149. (display (* mynum mynum))
150. )
151. ))))
152. ; kiem tra co phai nam nhuan ko
153. (define (divisibleBy? number divisor)
154. (= (remainder number divisor) 0))
155. (define (isBissextile? year)
156. (or (divisibleBy? year 400)
157. (and (divisibleBy? year 4)
158. (not (divisibleBy? year 100)))))
159. ; bai toan thap ha noi
160. (define (hanoi n A B C)
161. (if (= n 0)
162. (display "")
163. (begin
164. (hanoi (- n 1) A C B)
165. (display "move ") (display A) (display " to ") (display C) (newline)
166. (hanoi (- n 1) B A C))))
167. (define (bai14 x y)
168. (let ( (tbp (sqrt (+ (* x x) (* y y)))) (hbp (sqrt(- (* x x) (* y y)))))
169. (/ (- tbp hbp) (+ 1 tbp hbp))))
170. ; ham power n x lap
171. (define (iterpower n x kq)
172. (cond
173. ((= n 0) kq)
174. ((> n 0) (iterpower (- n 1) x (* kq x)))
175. (else (iterpower (- 0 n) (/ 1 x) kq))))
176.
177. (define (power n x)
178. (iterpower n x 1))
179. ;power de quy
180. (define (power2 n x)
181. (if (not (integer? n))
182. (display "Error")
183. (if (< n 0)
184. (power2 (- 0 n) (/ 1 x))
185. (if (= n 0) 1
186. (* x (power2 (- n 1) x))
187. )
188. )
189. )
190. )
191. ;tinh ra so giay tu
192. (define (nbsec h m s)
193. (+ (* h 3600) (* m 60 ) s))
194. ; tinh giai thua
195. (define (fac n)
196. (if (zero? n) ; hoặc (<= n 0) để đảm bảo xử lý mọi số n
197. 1
198. (* n (fac (- n 1)))))
199. ; ham fibonaci
200. (define (fib n)
201. (if (<= n 1)
202. n
203. (+ (fib (- n 1)) (fib (- n 2)))))
205. (begin (display "Enter 0 to quit, 1 to job1, 2 to job2.")
207. (cond ((equal? rd 0) (display "Good bye!"))
208. ((equal? rd 1) ((display "I work the job 1.")
210. ((equal? rd 2) ((begin (display "I work the job 2.")
212. (else ( (display "restart :un known command.")
214. ;test do
215.
216. (define a (list 1 2 3))
217. (write (cons a (list 4 5)))
218. ;danh sach
219. (define (mylength l)
220. ( if (null? l)
221. 0
222. (+ 1 (mylength (cdr l)))))
223.
224. (define (myreverse L)
225. (if (null? L)
226. '() ; hoặc (list)
227. (append (myreverse (cdr L)) (list (car L)))))
228.
229. ;Ví dụ minh hoạ sau đây định nghĩa một hàm dịch các đại từ nhân xưng t
230. ;tiếng Anh :
232. (case pronounF
233. ((je) 'I)
234. ((tu vous) 'you)
235. ((il) 'he)
236. ((elle) 'she)
237. ((nous) 'we)
238. ((ils) 'they)
239. (else 'ronoun-inconnu)))
241.
242. (define vec '#())
243. (define n 0)
244. (define i 0)
245. (define x 1)
246. (define p 1)
247. (define (Hoocner)
248. (begin
249. (display "Nhap bac n cua da thuc :")
251. (set! vec (make-vector (+ n 1)))
252. (display "Nhap cac he so cua da thuc :")
253. (newline)
254. (while (<= i n)
255. (begin
256. (display "a") (display i) (display ":")
258. (set! i (+ i 1))
259. ))
260. (display "Nhap gia tri x can tinh :")
262. ;Bat dau tinh da thuc
263. ;i=n
264. (set! i (- n 1))
265. ;p=a[n]
266. (set! p (vector-ref vec n))
267. ;i=n-->1
268. ;p=p*c + a[i]
269. (while (>= i 0)
270. (begin
271. (set! p (+ (* p x) (vector-ref vec i)))
272. (set! i (- i 1))
273. ))
274. (display "Gia tri cua bieu thuc :") (display p)
275.
276.
277. ))