(* Jamie's coursework template My Name here, My UserID, My Regno (why not) <--- so we know who you are F28PL Coursework ML1 <--- sanity check You may assume variables, procedures, and functions defined in earlier questions in your answers to later questions, though you should add comments in code explaining this if any clarification might help read your code. The file extension of this template is .txt. It should be .ml, but this does not play nice with TurnItIn. *) (* ################################################################################ *) (* Question 1. Complex number arithmetic. <--- Yes, so we know what question you think you're answering *Always have the question under your nose* The complex numbers are explained here (and elsewhere): http://www.mathsisfun.com/algebra/complex-number-multiply.html Represent a complex integer as an element of the datatype datatype cint = CI of int * int. (So CI(4,5) represents 4+5i.) Implement functions cadd and cmult of type cint * cint -> cint representing complex integer addition and multiplication. For instance, cadd(CI(1,0),CI(0,1)) should compute CI(1,1). *) (* cadd, and tests *) (* cmult, and tests *) (* END ANSWER TO Question 1 *) (* ################################################################################ *) (* Question 2. Sequence arithmetic An integer sequence is an element of type intseq = int list. (So intseq is a type alias for a list of integers.) Implement recursive functions seqadd and seqmult of type intseq * intseq -> intseq that implement pointwise addition and multiplication of integer sequences. For instance seqadd([1,2,3],[~1,2,2]) should compute [0,4,5] Do not write error-handling code to handle the cases that sequences have different lengths. *) (* END ANSWER TO Question 2 *) (* ################################################################################ *) (* Question 3. Matrices Matrix addition and multiplication are described here: addition: defhttp://www.mathsisfun.com/algebra/matrix-introduction.html Multiplication (dot product): 2ahttp://www.mathsisfun.com/algebra/matrix-multiplying.html Represent integer matrices as the datatype intmatrix = IM of intseq list. So a matrix is a column of rows of integers. Write functions ismatrix : intmatrix -> bool This should test whether a list of lists of integers represents a matrix (so the length of each row should be equal). matrixshape : intmatrix -> (int * int) This should return a pair that is the number of columns, and the number of elements in any row. matrixadd : intmatrix * intmatrix -> intmatrix Matrix addition, which is simply pointwise addition. You may find your previous answers useful. matrixmult : intmatrix * intmatrix -> intmatrix Similarly for matrix multiplication. Do not write error-handling code for malformed input, e.g. a column of rows of integers of different lengths, or an attempt to sum matrices of different shapes. *) (* END ANSWER TO Question 3 *) (* ################################################################################ *) (* Question 4. Essay-style question Write an essay on the ML type system. Be clear, to-the-point, and concise. Convince your marker that you understand: * Ad-hoc and parametric polymorphism. * Function types. * List types and tuple types (and their differences). * Equality types. * ML patterns and pattern-matching. Include short code-fragments (as I do when lecturing) to illustrate your observations. *) (* END ANSWER TO Question 4 *) (* ################################################################################ *) (* Question 5. Bonus question Write a pair of functions of types ((‘a * ‘b) -> ‘c) -> (‘a -> (‘b -> ‘c)) and (‘a -> ‘b -> ‘c) -> ((‘a * ‘b) -> ‘c) and explain why this was a cool question. *) (* END ANSWER TO Question 5 *) (* ################################################################################ *) (* Question 6. Seriously cool bonus question Write a pair of functions of types int -> (‘a -> ‘a) -> ‘a -> ‘a and ((int -> int) -> int -> ‘a) -> 'a (Hint: search for “Church numerals”.) *) (* END ANSWER TO Question 6 *) (* ################################################################################ *) (* 7. Unmarked question Implement the Tower of Hanoi as a function of type unit -> (int list*int list*int list) Implement Bubblesort and Quicksort in ML. *) (* END ANSWER TO Question 7 *)