Programming Assignment 3
CS 31600
Programming Languages
Spring, 2021

This assignment makes use of the files contained in this zip file. This assignment is due Tuesday, March 9.

This is your second programming assignment using ML.

Do the following Exercises from pages 115-116 from our textbook.

Also do the following modified versions of Exercises 6 and 7 from pages 116-117.

Exercise 6': Write a function insert that takes as input a number and a sorted list of numbers and outputs a list with the number inserted into the list at the correct place so that the returned list is still sorted. Do this recursively and use pattern matching. Hint: You need to consider only three cases: the given list is empty, the given number is less than the first element of the list, or the given number is greater than the first element of the list.

Exercise 6'': Use your insert function to write a sort function (a recursive version of "insertion sort"). Hint: To sort a list you insert the first element of the list into the sorted rest of the list.

Exercise 7': Do Exercise 7 but with your sort function from Exercise 6'' instead of the book's quicksort function. You will need to modify your version of insert so that it takes a comparison function as a parameter. Use the names sort2 and insert2 for the new versions of sort and insert. (Notice that sort2 and insert2 are somewhat like "generic" functions in Java or "template" functions in C++.)

Exercise 8': Write a function twiddle that takes as input a list of triples, and returns a list of pairs where each triple from the input has its middle element removed and its first and third elements swapped. Write this using pattern matching.

Exercise 9': Write a function flipper that flips alternate elements from a list. So if the input list is [a1, a2, a3, a4, a5, a6], then the output list should be [a2, a1, a4, a3, a6, a5]. If the length of the input list is odd, then the last element of the input list should also be the last element of the output list. Write this using pattern matching and the cons operator.

Put your solutions to these problems in the file called hw3.sml. Be sure to test your solutions to these problems using the file hw3-tests.sml. When you run the tests your output should (more or less) match the contents of the file hw3-tests-output.txt (use one of the included file comparison programs). You should test your code with additional tests, but don't put your tests in the file hw3.sml. If you want, you can submit another file hw3-my-tests.sml that contains your additional tests.

Make sure that your file hw3.sml begins with a ML comment block of the form

 (*
    CS 31600
    Hw 3
    Your_Name_Here
    Your_Email_Address_Here

    Any other comments that you want to make...
 *)

Turn in a zip file called CS316Hw3Surname.zip (where Surname is your last name) containing your version of hw3.sml (and, if you want to, hw3-my-tests.sml).

This assignment is due Tuesday, March 9.