Advertisement
Guest User

Untitled

a guest
Mar 1st, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. ; (define f-imperative (y) (x) ; x is a local variable
  2. ; (begin
  3. ; (set x e)
  4. ; (while (p x y)
  5. ; (set x (g x y)))
  6. ; (h x y)))
  7.  
  8.  
  9.  
  10.  
  11. ; Problem 9
  12.  
  13. ; 9b
  14. (define maxl* (l)
  15. (foldl max (car l) l))
  16.  
  17. (check-expect (maxl* '(5 7 1 3 9 2)) 9)
  18.  
  19. ; 9c
  20. (define gcd* (l)
  21. (foldl gcd 0 l))
  22.  
  23. (gcd* '(-5 -10 -20))
  24.  
  25. (check-expect (gcd* '(10 15 20)) 5)
  26.  
  27. ; 9d
  28. (define lcm* (l)
  29. (foldl lcm (car l) l))
  30.  
  31. (check-expect (lcm* '(2 5 8 10)) 40)
  32.  
  33. ; 9e
  34. (define sum (l)
  35. (foldl + 0 l))
  36.  
  37. (check-expect (sum '(1 2 3 4 5)) 15)
  38.  
  39. ; 9f
  40. (define product (l)
  41. (foldl * 1 l))
  42.  
  43. (check-expect (product '(1 2 3 4)) 24)
  44.  
  45. ; 9g
  46. (define append (l1 l2)
  47. (foldr cons l2 l1))
  48.  
  49. (check-expect (append '(1 2 3) '(4 5 6)) '(1 2 3 4 5 6))
  50.  
  51. ; 9i
  52. (define reverse (l)
  53. (foldl cons '() l))
  54.  
  55. (check-expect (reverse '(1 2 3 4 5)) '(5 4 3 2 1))
  56.  
  57. ; Problem 10
  58.  
  59. (define length (l)
  60. (let ((combine (lambda (x a) (+ 1 a))))
  61. (foldr combine 0 l)))
  62.  
  63. (check-expect (length '(1 2 3 4 5)) 5)
  64.  
  65. (define even? (n) (= 0 (mod n 2)))
  66.  
  67. (define map (f xs)
  68. (let ((combine (lambda (y ys) (cons (f y) ys))))
  69. (foldr combine '() xs)))
  70.  
  71. (check-expect (map ((curry +) 3) '(1 2 3 4 5 6)) '(4 5 6 7 8 9))
  72.  
  73. (define filter (f xs)
  74. (let
  75. ((combine (lambda (y ys) (if (f y) (cons y ys) ys))))
  76. (foldr combine '() xs)))
  77.  
  78. (check-expect (filter even? '(1 2 3 4)) '(2 4))
  79.  
  80. (define exists? (p? xs)
  81. (let ((find (lambda (y a) (if (p? y) (+ 1 a) a))))
  82. (if (< 0 (foldr find 0 xs))
  83. #t
  84. #f)))
  85.  
  86. (check-expect (exists? even? '(1 3 5 6)) #t)
  87. (check-expect (exists? even? '(1 3 5)) #f)
  88.  
  89. (define all? (p? xs)
  90. (let ((find (lambda (y a) (if (p? y) a (+ 1 a)))))
  91. (if (< 0 (foldr find 0 xs))
  92. #f
  93. #t)))
  94.  
  95. (check-expect (all? even? '(2 4 6 8)) #t)
  96. (check-expect (all? even? '(1 2 4)) #f)
  97.  
  98. ; Problem 15
  99.  
  100.  
  101.  
  102. ; Problem S
  103.  
  104. (define quicksort (c l1)
  105. (if (null? l1)
  106. '()
  107. (let ((pivot (car l1)))
  108. (foldr cons (foldr cons (quicksort c (filter (lambda (x) (c x pivot)) l1))
  109. (filter (lambda (x) (= x pivot)) l1))
  110. (quicksort c (filter (lambda (x) (c pivot x)) l1))))))
  111.  
  112.  
  113. (quicksort > '(2 4 3 1 5))
  114. (quicksort < '(2 4 3 1 5))
  115.  
  116. ; Problem A
  117. (define f-functional (y)
  118. (let* ((x e))
  119. (if (o not p)
  120. (h x y)
  121. (let* ((x (g x y)))
  122. (f-functional y)))))
  123.  
  124. ; Problem 15
  125.  
  126. (val emptyset (lambda (x) #f))
  127. (define member? (x s) (s x))
  128.  
  129. (define add-element (x s)
  130. (lambda (n)
  131. (if (s x)
  132. #t
  133. (if (equal? n x)
  134. #t
  135. #f))))
  136.  
  137. (define union (s1 s2)
  138. (lambda (x)
  139. (if (s1 x)
  140. #t
  141. (if (s2 x)
  142. #t
  143. #f))))
  144.  
  145. (define inter (s1 s2)
  146. (lambda (x)
  147. (if (s1 x)
  148. (if (s2 x)
  149. #t
  150. #f)
  151. #f)))
  152.  
  153. (define diff (s1 s2)
  154. (lambda (x)
  155. (if (s1 x)
  156. (if (s2 x)
  157. #f
  158. #t)
  159. #f)))
  160.  
  161.  
  162. (check-expect ((inter atom? number?) 1) #t)
  163. (check-expect ((inter atom? number?) #f) #f)
  164. (check-expect ((union atom? number?) #f) #t)
  165. (check-expect ((union atom? number?) '(1 2 3)) #f)
  166. (check-expect ((diff atom? number?) #f) #t)
  167. (check-expect ((diff atom? number?) 1) #f)
  168.  
  169. (val mk-set-ops (lambda (eqfun)
  170. (list6
  171. (lambda (x) #f)
  172. (lambda (x s) (s x))
  173. (lambda (n)
  174. (if (s x)
  175. #t
  176. (if (equal? n x)
  177. #t
  178. #f)))
  179. (lambda (x)
  180. (if (s1 x)
  181. #t
  182. (if (s2 x)
  183. #t
  184. #f)))
  185. (lambda (x)
  186. (if (s1 x)
  187. (if (s2 x)
  188. #t
  189. #f)
  190. #f))
  191. (lambda (x)
  192. (if (s1 x)
  193. (if (s2 x)
  194. #f
  195. #t)
  196. #f)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement