SHARE
TWEET

Untitled

a guest Jun 19th, 2017 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use "./test_util.sml";
  2. use "./ex5.sml";
  3.  
  4. (* -------------------- intbseq --------------- *)
  5.  
  6. fun composeNTimes f 1 = f
  7.     | composeNTimes f n = f o (composeNTimes f (n-1));
  8.  
  9. val a = intbseq 2;
  10. REQUIRE ("intbseq // staff #1") ((bHead a) = 2);
  11. val a = bForward a;
  12. REQUIRE ("intbseq // staff #2") ((bHead a) = 3);
  13. val a = bForward a;
  14. REQUIRE ("intbseq // staff #3") ((bHead a) = 4);
  15. val a = bBack a;
  16. REQUIRE ("intbseq // staff #4") ((bHead a) = 3);
  17. val a = bBack a;
  18. REQUIRE ("intbseq // staff #5") ((bHead a) = 2);
  19. val a = bBack a;
  20. REQUIRE ("intbseq // staff #6") ((bHead a) = 1);
  21. val a = bBack a;
  22. REQUIRE ("intbseq // staff #7") ((bHead a) = 0);
  23. val a = bBack a;
  24. REQUIRE ("intbseq // staff #8") ((bHead a) = ~1);
  25.  
  26. val a = intbseq 10;
  27. REQUIRE ("intbseq // #1") ((bHead a) = 10);
  28. val a = (composeNTimes bForward 10) a;
  29. REQUIRE ("intbseq // #2") ((bHead a) = 20);
  30. val a = (composeNTimes bForward 100) a;
  31. REQUIRE ("intbseq // #3") ((bHead a) = 120);
  32. val a = (composeNTimes bBack 100) a;
  33. REQUIRE ("intbseq // #4") ((bHead a) = 20);
  34. val a = (composeNTimes bBack 5) a;
  35. REQUIRE ("intbseq // #5") ((bHead a) = 15);
  36.  
  37. (* -------------------- bmap ---------------- *)
  38.  
  39. val a = bmap (fn x =>x*x) (intbseq 2);
  40. REQUIRE ("bmap // staff #1") ((bHead a) = 4);
  41. val a = bForward a;
  42. REQUIRE ("bmap // staff #2") ((bHead a) = 9);
  43. val a = bForward a;
  44. REQUIRE ("bmap // staff #3") ((bHead a) = 16);
  45. val a = bBack a;
  46. REQUIRE ("bmap // staff #4") ((bHead a) = 9);
  47. val a = bBack a;
  48. REQUIRE ("bmap // staff #5") ((bHead a) = 4);
  49. val a = bBack a;
  50. REQUIRE ("bmap // staff #6") ((bHead a) = 1);
  51. val a = bBack a;
  52. REQUIRE ("bmap // staff #7") ((bHead a) = 0);
  53. val a = bBack a;
  54. REQUIRE ("bmap // staff #8") ((bHead a) = 1);
  55. val a = bBack a;
  56. REQUIRE ("bmap // staff #9") ((bHead a) = 4);
  57.  
  58.  
  59.  
  60. val a = bmap (fn x => x div 2) (intbseq 0);
  61. REQUIRE ("bmap // #1") ((bHead a)=0);
  62. val a = bForward a;
  63. REQUIRE ("bmap // #2") ((bHead a)=0);
  64. val a = bForward a;
  65. REQUIRE ("bmap // #3") ((bHead a)=1);
  66. val a = bForward a;
  67. REQUIRE ("bmap // #4") ((bHead a)=1);
  68. val a = bForward a;
  69. REQUIRE ("bmap // #5") ((bHead a)=2);
  70. val a = bBack a;
  71. REQUIRE ("bmap // #6") ((bHead a)=1);
  72. val a = bForward a;
  73. REQUIRE ("bmap // #7") ((bHead a)=2);
  74. val a = bBack a;
  75. REQUIRE ("bmap // #8") ((bHead a)=1);
  76. val a = bBack a;
  77. REQUIRE ("bmap // #9") ((bHead a)=1);
  78. val a = bBack a;
  79. REQUIRE ("bmap // #10") ((bHead a)=0);
  80. val a = bBack a;
  81. REQUIRE ("bmap // #11") ((bHead a)=0);
  82. val a = bBack a;
  83. REQUIRE ("bmap // #12") ((bHead a)= (~1));
  84.  
  85. val a = bmap (fn x => x*2) (intbseq 0);
  86. REQUIRE ("bmap // #13") ((bHead a)=0);
  87. val a = (composeNTimes bForward 10) a;
  88. REQUIRE ("bmap // #14") ((bHead a)=20);
  89. val a = (composeNTimes bForward 10) a;
  90. REQUIRE ("bmap // #15") ((bHead a)=40);
  91. val a = (composeNTimes bBack 20) a;
  92. REQUIRE ("bmap // #16") ((bHead a)=0);
  93.  
  94.  
  95.  
  96. (* -------------------- bfilter ---------------- *)
  97.  
  98. val a = bfilter (fn x => x mod 2 = 0) Back (intbseq 2);
  99. REQUIRE ("bfilter // staff #1") ((bHead a)=2);
  100. val a = bForward a;
  101. REQUIRE ("bfilter // staff #2") ((bHead a)=4);
  102. val a = bForward a;
  103. REQUIRE ("bfilter // staff #3") ((bHead a)=6);
  104. val a = bBack a;
  105. REQUIRE ("bfilter // staff #4") ((bHead a)=4);
  106. val a = bBack a;
  107. REQUIRE ("bfilter // staff #5") ((bHead a)=2);
  108. val a = bBack a;
  109. REQUIRE ("bfilter // staff #6") ((bHead a)=0);
  110. val a = bBack a;
  111. REQUIRE ("bfilter // staff #7") ((bHead a)=(~2));
  112. val a = bfilter (fn x => x mod 2 = 0) Back (intbseq 1);
  113. REQUIRE ("bfilter // staff #8") ((bHead a)=0);
  114. val a= bfilter (fn x => x mod 2 = 0) Forward (intbseq 1);
  115. REQUIRE ("bfilter // staff #9") ((bHead a)=2);
  116.  
  117. fun isPrime x = let
  118.     fun aux x 1 = true
  119.         | aux x m = if (x mod m = 0) then false else (aux x (m-1));
  120.     in (aux x (Real.ceil (Math.sqrt (real x)))) end;
  121.  
  122. val a = bfilter (fn x => (isPrime x)) Back (intbseq 11);
  123. REQUIRE ("bfilter // #1") ((bHead a)=11);
  124. val a = bForward a;
  125. REQUIRE ("bfilter // #2") ((bHead a)=13);
  126. val a = bForward a;
  127. REQUIRE ("bfilter // #3") ((bHead a)=17);
  128. val a = bForward a;
  129. REQUIRE ("bfilter // #4") ((bHead a)=19);
  130. val a = bBack a;
  131. REQUIRE ("bfilter // #5") ((bHead a)=17);
  132. val a = bBack a;
  133. REQUIRE ("bfilter // #6") ((bHead a)=13);
  134. val a = bBack a;
  135. REQUIRE ("bfilter // #7") ((bHead a)=11);
  136. val a = bForward a;
  137. REQUIRE ("bfilter // #8") ((bHead a)=13);
  138. val a = bForward (bForward (bForward a));
  139. REQUIRE ("bfilter // #9") ((bHead a)=23);
  140. val a = bForward (bForward (bForward a));
  141. REQUIRE ("bfilter // #10") ((bHead a)=37);
  142. val a = (composeNTimes bForward 10) a;
  143. REQUIRE ("bfilter // #11") ((bHead a)=79);
  144. val a = (composeNTimes bForward 20) a;
  145. REQUIRE ("bfilter // #12") ((bHead a)=181);
  146. val a = (composeNTimes bBack 30) a;
  147. REQUIRE ("bfilter // #13") ((bHead a)=37);
  148.  
  149.  
  150.  
  151. (* -------------------- seq2bseq ---------------- *)
  152.  
  153. fun from(x) = Cons(x, fn()=>from(x+1));
  154. fun downfrom(x) = Cons(x, fn()=>downfrom(x-1));
  155. val a = seq2bseq (downfrom ~1) (from 0);
  156.  
  157. REQUIRE("seq2bseq // staff #1") ((bHead a)=0);
  158. val a = bForward a;
  159. REQUIRE("seq2bseq // staff #2") ((bHead a)=1);
  160. val a = bBack a;
  161. REQUIRE("seq2bseq // staff #3") ((bHead a)=0);
  162. val a = bBack a;
  163. REQUIRE("seq2bseq // staff #4") ((bHead a)=(~1));
  164.  
  165. val a = seq2bseq (downfrom ~1) (from 0);
  166. REQUIRE ("seq2bseq // #1") ((bHead a)=0);
  167. val a = (composeNTimes bForward 10) a;
  168. REQUIRE ("seq2bseq // #2") ((bHead a)=10);
  169. val a = (composeNTimes bBack 10) a;
  170. REQUIRE ("seq2bseq // #3") ((bHead a)=0);
  171.  
  172. fun bseq2seq b Forward = Cons((bHead b), fn () => (bseq2seq (bForward b) Forward))
  173.     | bseq2seq b Back = Cons((bHead b), fn () => (bseq2seq (bBack b) Back));
  174. val positivePrimesOver11 = bseq2seq (bfilter (fn x => (isPrime x)) Back (intbseq 11)) Forward;
  175. val oddsUnder11 = bseq2seq (bfilter (fn x => (x mod 2 = 1)) Back (intbseq 10)) Back;
  176.  
  177. val a = seq2bseq oddsUnder11 positivePrimesOver11;
  178. REQUIRE ("seq2bseq // #4") ((bHead a)=11);
  179. val a = bForward a;
  180. REQUIRE ("seq2bseq // #5") ((bHead a)=13);
  181. val a = bBack (bBack a);
  182. REQUIRE ("seq2bseq // #6") ((bHead a)=9);
  183. val a = (composeNTimes bForward 10) a;
  184. REQUIRE ("seq2bseq // #7") ((bHead a)=43);
  185. val a = (composeNTimes bBack 9) a;
  186. REQUIRE ("seq2bseq // #8") ((bHead a)=11);
  187. val a = (composeNTimes bBack 4) a;
  188. REQUIRE ("seq2bseq // #9") ((bHead a)=3);
  189.  
  190.  
  191. (* -------------------- bSeqJump ---------------- *)
  192.  
  193.  
  194. val a = bSeqJump (intbseq 0) 3;
  195. REQUIRE ("bSeqJump // staff #1") ((bHead a) = 0);
  196. val a = bForward a;
  197. REQUIRE ("bSeqJump // staff #2") ((bHead a) = 3);
  198. val a = bForward a;
  199. REQUIRE ("bSeqJump // staff #3") ((bHead a) = 6);
  200. val a = bBack a;
  201. REQUIRE ("bSeqJump // staff #4") ((bHead a) = 3);
  202. val a = bBack a;
  203. REQUIRE ("bSeqJump // staff #5") ((bHead a) = 0);
  204. val a = bBack a;
  205. REQUIRE ("bSeqJump // staff #6") ((bHead a) = (~3));
  206.  
  207. val a = bSeqJump (bfilter (fn x => (isPrime x)) Back (intbseq 3)) 3;
  208. REQUIRE ("bSeqJump // #1") ((bHead a) = 3);
  209. val a = bForward a;
  210. REQUIRE ("bSeqJump // #2") ((bHead a) = 11);
  211. val a = bForward a;
  212. REQUIRE ("bSeqJump // #3") ((bHead a) = 19);
  213. val a = bBack (bBack a);
  214. REQUIRE ("bSeqJump // #4") ((bHead a) = 3);
  215.  
  216.  
  217. val a = bSeqJump (bfilter (fn x => x mod 2 = 0) Back (intbseq 0)) 3;
  218. REQUIRE ("bSeqJump // #5") ((bHead a) = 0);
  219. val a = (composeNTimes bForward 10) a;
  220. REQUIRE ("bSeqJump // #6") ((bHead a) = 60);
  221. val a = (composeNTimes bBack 10) a;
  222. REQUIRE ("bSeqJump // #7") ((bHead a) = 0);
  223. val a = (composeNTimes bForward 50) a;
  224. REQUIRE ("bSeqJump // #8") ((bHead a) = 300);
RAW Paste Data
Top