Department of Computer Science
Functional Languages
Fall Semester 2010
Dr. Maury Eggen
Homework Laboratory Assignment 6:
The purpose of this assignment is to familiarize you with some of the
fundamental programming techniques of the ML lamguage. As such, we shall
write several functions which operate on tuples and lists:
- 1. flip(ls); Write a function which flips alternate elements of a list. If n is odd, the last element remains fixed.
- 2. ordertuple(ls); Write a function which takes a list of pairs of integers and orders the elements of each pair such that the smaller number is first. Use the as construct. (note: pairs are tuples).
- 3. orderlist(ls); Write a function which takes a list of lists of two integers and orders the elements of each pair such that the smaller number is first.
- 4. delete(i,L); Write a function that, given a list L and an integer i, returns a copy of L with the ith element deleted. If i is greater than the length of L, return L.
- 5. member(x,S); Suppose we represent sets by lists. Write functions member(x,S) which will return true if x is in S and false otherwise;
- 6. delete(x,S); a function delete(x,S) which will remove x from S;
- 7. insert(x,s); a function insert(x,S) which will insert x into S (if it is not already present). Note that sets contain each element only once, and that sets can be in any order.
- 8. insertfront(a,L); Write a function which takes an element a and a list L of lists of elements of the same type as a and inserts a onto the front of each of the lists in the list L. Make sure nil also works.
- 9. sumtuple(lst); Write a function which will take a list of pairs (tuples) as argument and will return a tuple whose first item is the sum of all of the first items, and the second item is the sum of all of the second items.
- 10. sumevenodd(lst); Write a function which will take a list as argument and will return a pair consisting of the sum of the even positions and the sum of the odd positions.
- 11. Cat(L,M); Write a function Cat(L,M) which produces the concatenation L@M of the lists L and M. Your function may not use the @ operator, only ::.
- 12. Power(S); Suppose we represent sets by lists. Write a function which will take a set (list) S as argument, and will return the power set of S. If S = [1,2] then your function is to return [nil, [1], [2], [1,2]]. Hint: use recursion on the tail of the list, and apply problem 6.
24 points.