CS 31600 - Homework Assignment 5

This assignment is your second assignment using ML. This assignment is due Thursday, March 22.

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

  • Exercise 2
  • Exercise 3
  • Exercise 4

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. (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").

Exercise 7': Do Exercise 7 but with your sort function instead of the book's quicksort function. You will need to modify your version of insert. Give examples of using your new sort function to sort a list of integers, and also a list of reals. (What about a list of char?). Notice that this is somewhat like a "generic" function in Java, or a "template" function 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.

Put your solutions to these problems in a file called CS316Hw5Surname.sml (where Surname is your last name). Be sure to test your solutions to these problems, but don't put your tests in the file CS316Hw5Surname.sml (I will provide my own tests of your solutions.). If you want, you can submit another file CS316Hw5Surname-tests.sml that contains your tests.

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

(*
   CS 31600
   Your Name Here
   Assignment 5

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

Turn in a zip file called CS316Hw5Surname.zip (where Surname is your last name) containing the ML file CS316Hw5Surname.sml (and, if you want, your file CS316Hw5Surname-tests.sml).

This assignment is due Thursday, March 22.


Return to the main homework page.
Return to the CS 31600 home page.


compliments and criticisms