Problem Set 4: Working with Lists
Assigned: Tuesday February 9, 2016
Due: Thursday February 18 midnight, 2016
Points: 14 up to 18 Points
This problem set involves writing small functions working with
lists and tuples. Begin by downloading the
harness code. Expand this archive
and store the resulting folder in a reasonable place. Then do any
of the problems totalling 14 points. Feel free to do extras totalling
up to 18 points.
This code uses automated testing so you'll need to run the code
in the same way as with code that uses the Universe graphics
library. In particular, open a unix shell and type
> make
> ./problemset4
to try out your solutions.
- (1 Point) Write a function squares : int list -> int list
such that a call (squares [n1; n2; ...;
nk]) returns the
list of squares [n12;
n22; ...; nk2]).
- (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 invoke the failwith "msg" form.
- (1 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.
- (1 Point) Write a function addEvens : int list ->
int. Given the call addEvens [1; 2; 3; 4]
the addEvens function should return 6.
- (2 Points) The
Hamming Distance
between two
strings is the number of characters that differ. For example,
the Hamming Distance bewteen "ABCD" and "ABDE" is 2. Write a
function hammingDistance : string -> string -> int such
that (hammingDistance s1 s2) returns the Hamming Distance
between s1 and s2.
- (2 Points) 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.
- (2 Points) 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 Points) Write a function removeNth : 'a list -> int -> 'a list
such that a call (removeNth xs n) removes the nth element of
xs, starting with an index of 0. For example, the call
(removeNth [10; 30; 20] 1) should evaluate to
[10; 20].
- (2 Points) 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 Points) Write a function
partition : 'a -> 'a list -> ('a list) * ('a list)
such that a call (partition x xs) returns a pair
of lists (left, right) such that all of the elements
in left are less than or equal to x and all
of the elements in right are greater than x.
- (2 Points) 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 Points) 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 Points) Write a function
zip : ('a list * 'b list) -> ('a * 'b) list
such that a call (zip ([x1; ...; xn], [y1; ...; yn]))
returns the list of pairs [(x1, y1); ...; (xn, yn)].
You may assume that the input lists are of the same length.
- (2 Points) Write a function
unzip : ('a * 'b) list -> ('a list * 'b list)
such that a call (zip [(x1, y1); ...; (xn, yn)])
returns the pair ([x1; ...; xn], [y1; ...; yn])).
- (3 Points) Write a function
powerset : 'a list -> ('a list) list which returns
the list of all sublists of the input argument.
Given the call powerset [1; 2; 3] the powerset
function should return the list of lists
[[1; 2; 3]; [1; 2]; [2; 3]; [1; 3]; [1]; [2]; [3]; []]
(no necessarily in that order).
- (4 Points) The
Sumerians developed an innovative
numeral system using base 60 (
sexagesimal) rather than
our familiar base 10 (decimal). The Sumerian numeral
system accounts for many common features of modern life
including the 60 minutes in an hour, the 60 seconds in a
minute, the measure of angles and geographic
coordinates. There are many theories as to why the
Sumerians chose 60 as their base. One widely believed
theory is that 60 was very practical because 60 has many
(i.e., 12) integer factors:
[1; 2; 3; 4; 5; 6; 10; 12; 15; 20; 30; 60]
so it allowed for a robust system of coinage.
Write a function howManyFactors : int list -> int list
such that a call howManyFactors ms returns a list
of pairs [(m1, n1);
(m2, n2); ...;
(mk, nk)],
where the input list ms is the list
[m1; m2; ...;
mk] and n1 is the
number of integer factors of m1,
n2 is the number of integer factors
of m2 and so on. For example, the
call howManyFactors [4; 5; 6] would return the
list [(4, 3); (5, 2); (6, 4)].
- (2 Points) 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. Feel free to convert the
input string to a list of characters using the following
explode function:
let explode s =
let rec exp i acc =
match i < 0 with
true -> acc
| false -> exp (i - 1) (s.[i] :: acc) in
exp (String.length s - 1) [];;
- (2 Points) An election was held and the results are in a list
[("Trump", 100); ("Carson", 5); ("Fiorina", 6)]
Write a function
winner : string * int list -> string.
Given the call
winner [("Trump", 100); ("Carson", 5); ("Fiorina", 6)],
the winner function should return the string "Trump".
|