Problem Set 4: Working with Lists and Tuples
Assigned: Friday February 12, 2016
Due: Friday February 19, 2016
Points: 14 points up to 18 points
This problem set involves writing small functions working with
lists and tuples. Begin by downloading the
harness code. Uncompress this archive
and store the resulting folder in a reasonable place. Then do any
of the problems totalling 14 points. Feel free to extras totalling
up to 18 points.
- (1 Point) Write a function last : 'a list -> 'a.
Given the call last([1, 2, 3]) the last function
should return 3. If last is called with an
empty list it should return an error message.
- (2 Point) Write a function addEvens : int list ->
int. Given the call addEvens([1, 2, 3, 4])
the addEvens function should return 6.
- (2 Point) Write a function
removeDuplicates : 'a list -> 'a list. Given the
call removeDuplicates([1, 2, 3, 3, 2])
the removeDuplicates function should return
[1, 2, 3].
- (2 Point) Write a function
isAscending : 'a list -> bool.
Given the call isAscending([1, 4, 4]),
the isAscending function should return False.
Given the call isAscending([8]) or isAscending([])
the isAscending function should return True.
- (3 Point) Write a function
powerList : 'a list -> ('a list) list which returns
the list of all sublists of the input argument.
Given the call powerList([1, 2, 3]) the powerList
function should return the list of lists
[[1, 2, 3], [1, 2], [2, 3], [1, 3], [1], [2], [3], []]
(no necessarily in that order).
- (2 Point) Write a function
insert : 'a -> 'a list -> 'a list.
Given the call insert(20, [10, 30, 50]) the insert
function should return the list
[10, 20, 30, 50]. In writing the insert function,
you may assume that the second argument is in ascending order.
- (2 Point) Write a function
merge : 'a list -> 'a list -> 'a list.
Given the call merge([20, 60], [10, 30, 50]) the
merge function should return the list
[10, 20, 30, 50, 60]. In writing the merge
function, you may assume that the two input lists are
in ascending order.
- (2 Point) Write a function
split : 'a list -> ('a list * 'a list) that
splits lists in half.
Given the call split([10, 5, 20, 40, 50]) the
split function should return the pair of lists
([10, 5], [20, 40, 50]).
- (2 Point) Write a function
almostInsertionSort : 'a list -> 'a list -> 'a list.
Given the call almostInsertionSort([40, 20], [10, 30, 50]),
the almostInsertionSort function should return the
sorted list [10, 20, 30, 40, 50]. In writing the
almostInsertionSort function, you may assume that the
second argument is in ascending order.
- (2 Point) Write a function
antiCons : 'a list -> ('a * 'a list).
Given the call antiCons(["Mary", "Alice", "Lisa"])
the antiCons function should return the pair
("Lisa", ["Mary", "Alice"]).
- (2 Point) Write a function
isPalindrome : string -> bool.
Given the call
isPalindrome("able was I ere I saw elba") the
isPalindrome function should return True.
Note that it is case-sensitive.
- (1 Point) Write a function
zip : ('a list * 'b list) -> ('a * 'b) list.
Given the call zip(["A", "B"], [26, 25]), the
zip function should return the list of pairs
[("A", 26), ("B", 25)]. You may not assume that
the input lists are of the same length.
- (1 Point) Write a function
unzip : ('a * 'b) list -> ('a list * 'b list).
Given the call unzip([("A", 26), ("B", 25)]) the
unzip function should return the pair of lists
(["A", "B"], [26, 25]). You may not assume that
the input lists are of the same length.
|