Assignment Three: ML Programs
Due 16th October 2012
Write the ML functions described in the problems below.
Problem One (Chapter 7 Exercise 6 in Webber)
Write a quicksort function of type int list -> int list. Here's a review of the quicksort algorithm. First pick an element and call it the pivot (The head of the list is an easy choice for the pivot). Partition the rest of the list into two sublists, one with all the elements less than the pivot and another with all the elements not less than the pivot. Recursively sort the sublists. Combine the two sublists (and the pivot) into a final sorted list.
- quicksort [5,1,3,2,8];
[1,2,3,5,8]
- quicksort [1,1,2,2,3,4,5,6,7,8,10,9];
[1,1,2,2,3,4,5,6,7,8,9,10]
Problem Two (Chapter 7 Exercise 8 in Webber)
Write a member function which tests whether an element is a member of a set where the set is specified as a list. Do not assume that you can sort the lists. Do assume that input lists have no duplicate elements and do guarantee that output lists have no duplicate elements.
- member (1,[1,3,5]);
true
- member (3,[1,2]);
false
- member (1,[]);
false
Problem Three (Chapter 7 Exercise 9 in Webber)
Write a union function which constructs the union of two sets where the sets are specified as lists. Do not assume that you can sort the lists. Do assume that input lists have no duplicate elements and do guarantee that output lists have no duplicate elements.
- union ([1,2,3],[1,3,5]);
[1,2,3,5]
- union ([1,2,3],[1,2]);
[1,2,3]
- union ([],[1,2]);
[1,2]
Problem Four (Chapter 7 Exercise 10 in Webber)
Write a intersection function which constructs the intersection of two sets where the sets are specified as lists. Do not assume that you can sort the lists. Do assume that input lists have no duplicate elements and do guarantee that output lists have no duplicate elements.
- intersection ([1,2,3],[1,3,5]);
[1,3]
- intersection ([1,2,3],[1,2]);
[1,2]
- intersection ([],[1,2]);
[]
Problem Five (Chapter 7 Exercise 11 in Webber)
Write a function to construct the powerset of any set. A set's powerset is the set of all of its subsets. Consider the set A = {1,2,3}. It has various subsets {1}, {1,2}, and so on. Of course the empty set, {}, is a subset of every set. The powerset of A is the set:
{ {}, {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3} }
Your powerset function should take a list (representing the set) and return a list of lists (representing the set of all subsets of the original set). Your powerset function need not work on the untyped empty list; it may give an error message when evaluating powerset nil. But it should work on a typed empty list, so powerset (nil : int list) should give the right answer ( [[]] ).
What to Do
Create one ML file which contains all the functions. Each function should be named as described above. The file should be saved as AssignmentThree. To turn it in, add it to your SVN repository for this course (The URL should be: https://svn.cs.du.edu/courses/comp3351/f2012/YOUR_USER_NAME).
Grading
The function for each problem will be graded out of 5 possible points.
There are a total of 25 points for this assignment.
The function for each problem will be graded as follows:
Does your function match the required description (name and number of paramters)? (1 point)
Does your function return correct results for the examples provided? (1 point)
Does your function return correct results for more complicated examples? (2 points)
Does your function return correct results for ALL of my examples? (1 point)