Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.90 KB | None | 0 0
  1. fun cadd (CI(x1,y1),CI(x2,y2)) = CI((x1+x2),(y1+y2));
  2. fun cmult (CI(x1,y1),CI(x2,y2)) = CI((x1*x2),(y1*y2));
  3. fun seqadd ([],[]) = [] | seqadd (lst1,lst2) = (hd lst1 + hd lst2)::(seqadd((tl lst1):intseq, (tl lst2):intseq):intseq);
  4. fun seqmult ([],[]) = [] | seqmult (lst1,lst2) = (hd lst1 * hd lst2)::(seqmult((tl lst1):intseq, (tl lst2):intseq):intseq);
  5.  
  6. (*********************************)
  7. (* Start of answer to Question 1
  8. Represent a complex integer as an element of the datatype
  9. datatype cint = CI of int * int.
  10. (So CI(4,5) represents 4+5i.)
  11.  
  12. Implement functions cadd and cmult of type cint * cint -> representing complex integer addition and
  13. multiplication.
  14. (So cadd(CI(1,0),CI(0,1)) should compute CI(1,1).)
  15. *)
  16.  
  17. (* before *)
  18. datatype cint = CI of int * int;
  19.  
  20. (* cadd *)
  21. fun cadd (CI(x1,y1),CI(x2,y2)) = CI((x1+x2),(y1+y2));
  22.  
  23. (* Test 1 (should return CI(1,1)) *)
  24. cadd(CI(3,2),CI(4,7));
  25.  
  26. (* Test 2 (should return CI(7,9)) *)
  27. cadd(CI(3,2),CI(4,7));
  28.  
  29. (* Test 3 (should return CI(6,~5)) *)
  30. cadd(CI(5,~6),CI(1,1));
  31.  
  32. (* Test 4 (should return an error as it is using reals) *)
  33. cadd(CI(0.3,0.2),CI(0.1,0.0));
  34.  
  35.  
  36. (* cmult *)
  37. fun cmult (CI(x1,y1),CI(x2,y2)) = CI((x1*x2),(y1*y2));
  38.  
  39. (* Test 1 (should return CI(0,0)) *)
  40. cmult(CI(1,0),CI(0,1));
  41.  
  42. (* Test 2 (should return CI(12,14)) *)
  43. cmult(CI(3,2),CI(4,7));
  44.  
  45. (* Test 3 (should return CI(5,~12)) *)
  46. cmult(CI(5,~6),CI(1,2));
  47.  
  48. (* Test 4 (should return an error as it is using reals) *)
  49. cmult(CI(0.3,0.2),CI(0.1,0.0));
  50.  
  51.  
  52. ( * End of answer to Question 1 * )
  53. (*********************************)
  54.  
  55.  
  56. (*********************************)
  57. (* Start of answer to Question 2
  58. Implement recursive functions seqadd and seqmult of type intseq * intseq -> intseq that implement
  59. pointwise addition and multiplication of integer sequences.
  60. (So seqadd([1,2,3],[~1,2,2]) should compute [0,4,5])
  61. *)
  62.  
  63. (* before *)
  64. type intseq = int list;
  65.  
  66. (* seqadd *)
  67. fun seqadd ([],[]) = []
  68. (* If the list is empty return an empty list *)
  69. | seqadd (lst1,lst2) = (hd lst1 + hd lst2)::(seqadd((tl lst1):intseq, (tl lst2):intseq):intseq);
  70.  
  71. (* Test 1 (should return [10, 36, 4]: intseq) *)
  72. seqadd([5,4,3],[5,32,1]);
  73.  
  74. (* Test 2 (should return [3, 5, ~5]: intseq) *)
  75. seqadd([1,7,~4],[2,~2,~1]);
  76.  
  77. (* Test 3 (should return [3]: intseq) *)
  78. seqadd([1],[2]);
  79.  
  80. (* Test 4 (should return an error as it is using a triad tuple) *)
  81. seqadd([1,4,5],[1,7,8],[3,6,5]);
  82.  
  83.  
  84. (* seqmult *)
  85. fun seqmult ([],[]) = []
  86. (* If the list is empty return an empty list *)
  87. | seqmult (lst1,lst2) = (hd lst1 * hd lst2)::(seqmult((tl lst1):intseq, (tl lst2):intseq):intseq);;
  88.  
  89. (* Test 1 (should return CI(0,0)) *)
  90. seqmult(CI(1,0),CI(0,1));
  91.  
  92. (* Test 2 (should return CI(12,14)) *)
  93. seqmult(CI(3,2),CI(4,7));
  94.  
  95. (* Test 3 (should return CI(5,~12)) *)
  96. seqmult(CI(5,~6),CI(1,2));
  97.  
  98. (* Test 4 (should return an error as it is using a triad tuple) *)
  99. seqmult([1,4,5],[1,7,8],[3,6,5]);
  100.  
  101.  
  102. ( * End of answer to Question 2 * )
  103. (*********************************)
  104.  
  105. (*********************************)
  106. (* Start of answer to Question 3
  107. Represent integer matrices as the datatype intmatrix = IM of intseq list.
  108. Write functions
  109. - ismatrix : intmatrix -> bool
  110. - matrixshape : intmatrix -> (int * int)
  111. - matrixadd : intmatrix * intmatrix -> intmatrix
  112. - matrixmult : intmatrix * intmatrix -> intmatrix
  113.  
  114. (* before *)
  115. type intseq = int list;
  116. datatype intmatrix = IM of intseq list;
  117. fun seqadd ([],[]) = []
  118. | seqadd (lst1,lst2) = (hd lst1 + hd lst2)::(seqadd((tl lst1):intseq, (tl lst2):intseq):intseq);
  119. (* Need to define seqadd as it is used in my implementation of matrixadd*)
  120.  
  121. (* ismatrix *)
  122. fun ismatrix (IM([x])) = true |
  123. (* if the list contains only one element return true *)
  124. ismatrix (IM(m)) = (length(hd m) = length(hd(tl m)) andalso ismatrix(IM(tl m)));
  125. (* recursivley compares the of a row with the row below it and then performs a boolean operation to check that all previous comparisons have come back true *)
  126.  
  127. (* Test 1 (should return true) *)
  128. ismatrix(IM([[3,4,5],[1,2,6],[3,4,5]]));
  129.  
  130. (* Test 2 (should return false) *)
  131. ismatrix(IM([[3,4,5],[1,6],[3]]));
  132.  
  133. (* Test 3 (should return true) *)
  134. ismatrix(IM([[3,4,5]]));
  135.  
  136. (* Test 4 (should return false as it is of the type int list list not intmatrix) *)
  137. ismatrix([[3,4,5],[1,2,6],[3,4,5]]);
  138.  
  139. (* matrixshape *)
  140. fun matrixshape (IM(m)) = ((length (hd m)),(length (m)));
  141. (* returns the length of the first row and therefore the overall width of the matrix along with
  142. the depth of the matrix *)
  143.  
  144. (* Test 1 (should return 3,3) *)
  145. matrixshape(IM([[3,4,5],[1,2,6],[3,4,5]]));
  146.  
  147. (* Test 2 (should return an exception as there is no matrix) *)
  148. matrixshape(IM([]));
  149.  
  150. (* Test 3 (should return 3,1) *)
  151. matrixshape(IM([[3,4,5]]));
  152.  
  153. (* matrixadd *)
  154. fun matrixadd ([],[]) = []
  155. (* returns an empty list if either list is empty *)
  156. | matrixadd (m1,m2) = seqadd((hd m1), (hd m2))::(matrixadd((tl m1), (tl m2)));
  157. (* recursively adds each row by using the seqadd function from the previous question and then appends the result to the new list *)
  158.  
  159. (* Test 1 (should return [[4, 7, 10], [3, 4, 7], [7, 6, 6]]) *)
  160. matrixadd(([[3,4,5],[1,2,6],[3,4,5]]),([[1,3,5],[2,2,1],[4,2,1]]));
  161.  
  162. (* Test 2 (should return [[4, 7, 10]]) *)
  163. matrixadd(([[3,4,5]]),([[1,3,5]]));
  164.  
  165. (* Test 3 (should return 3,1) *)
  166. matrixshape(IM([[3,4,5]]))
  167.  
  168. ( * End of answer to Question 3 * )
  169. (*********************************)
  170.  
  171. (*********************************)
  172. (* Start of answer to Question 4
  173. ML Type System Essay
  174. Convince the marker you understand:
  175. - Ad-hoc and parametric polymorphism.
  176. - Function types.
  177. - Pair types.
  178. - Equality types.
  179. - Type aliases and datatype declarations (and their differences)
  180. *)
  181.  
  182.  
  183. The ML Type system is a particularly strong static type system. Where it differs from many languages is the lack of subtypes. This essay aims to explain certain features of this rather unique type system. The first of which being Ad-hoc and parametric polymorphism. This feature allows the writing of more generic functions in that the functions can be applied to arguments of a different type. Ad hoc polymorphism is also known as overloading and allows for the use of the same operators on different types by using different implementations of the functions. Parametric Polymorphism in ML is achieved through the use of type variables to express unknown types which are represented using the Greek alphabet. Another interesting feature is the function types. Functions in ML map from what is known as the domain type to the range type in the following format, fn : domain type -> range type. The domain type being the parameters of the function and the range type being the result. Pairs in ML can come in variety of forms. Pairs allow you to combine types for instance the tuple (3, 7) would be of type (int * int). However they are not limited only to two expressions you could also have (3, 7, 5) which is known as a triple. It is also possible to mix types with (27, true, “spaghetti”) being of type (int * bool * string). The last thing that should be noted about pairs is the possibility of a zero tuple, () which is of type unit. Equality types using the ML system is defined as any type which allows equality testing. Real, streams and functions are the only types that are not Equality types. When comparing Equality types in ML we use = to check if the values either side are equal and use <> to check if the values are not equal. ‘’a is used to range over all equality types. Type aliases allow you to assign a type a synonym which can make types more meaningful. For example if you were writing a program to be used by a library in which each book had an id, you could use type book_id = int; this would allow you to set book_id as the explicit type in the program. The keyword datatype can be used to declare new data types, this differs from type in that it will create an entirely new data type rather than just providing a synonym for a pre-existing one. An example of a datatype declaration could be datatype flower = healthy | dead | wilting. A datatype declaration will introduce both new type constructors and also new value constructors for each of the type constructors. Perhaps the most interesting feature of defining datatypes is that they can be recursive. In conclusion the ML type system is a diverse and unique system that gives you a great deal of control over the types you are using while still remaining strong and statically typed.
  184.  
  185. ( * End of answer to Question 4 * )
  186. (*********************************)
  187.  
  188. (*********************************)
  189. (* Start of answer to Question 5
  190. Write a pair of function types
  191. (('a * 'b) -> 'c) -> ('a -> ('b -> 'c))
  192. and
  193. ('a -> 'b -> 'c) -> (('a * 'b) -> 'c)
  194. *)
  195.  
  196. fun myfunc1 f x y = f (x,y)
  197.  
  198. (* Allows you to input arguments serpratley into a function that originally would have taken a tuple of values as an argument *)
  199.  
  200. fun myfunc2 f (x,y) = f x y
  201.  
  202. (* Performs the inverse of the previous function *)
  203.  
  204. ( * End of answer to Question 5 * )
  205. (*********************************)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement