Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
Homework 1
This is a programming assignment in OCaml to be done individually. Download the accompanying
OCaml source file hw1.ml and enter your solutions in it where indicated. When you are done,
submit it through the Assignments section of ICON as a plain text file with the same name. Make
sure you write your name in that file where indicated.
Each of your answers must be free of static errors. You may receive no credit for problems whose
code contains syntax or type errors.
Pay close attention to the specification of each problem and the restrictions imposed on its solution.
Solutions ignoring the restrictions may receive only partial credit or no credit at all.
In the following problems do not use any mutable predefined types such as references, mutable
records, arrays, and so on. Define your functions recursively as needed and take advantage of the
match operator.
You do not need, and should not use, any OCaml library functions over lists except for built-in
operators [], ::, and @. However, you can use your own auxiliary functions if you prefer.
Do not worry about the efficiency of your solution. Strive instead for cleanness and simplicity.
Unnecessarily complicated code may not receive full credit.
1 Programming Functional Style with Lists
1. Write a function sum: int list -> int which takes as input a list l of integers and returns
the sum of all the elements in l.
For example, sum [10; 3; 5] is 18, while sum [] is 0.
2. Write a function pairsum: (int * int) list -> (int * int) which takes as input a list
l of integer pairs and returns the sum of all the pairs in l, where the sum of two pairs (x1, y1)
and (x2, y2) is (x1 + x2, y1 + y2).
For example, pairsum [(10,1); (3, 0); (5,-2)] is (18,-1), while pairsum [] is (0,0).
3. Write a parametric function zip: ’a list -> ’b list -> ’a * ’b list which takes two
lists of the same length and produces a list of pairs of corresponding elements in the two input
lists.
1
For example, zip [4; 2; 5; 9] ["a"; "b"; "c"; "d"] is
[(4, "a"); (2, "b"); (5, "c"); (9, "d")].
1
4. Write a parametric function drop: ’a -> ’a list -> ’a list which takes as input a value
x of type 0a and an 0a list l, and “drops” all the occurrences of x from l, that is, returns a list
containing, in the same order, all and only the elements of l that differ from x.
For example,
drop 2 [2; 1; 3; 3; 2; 1] is [1; 3; 3; 1],
drop 5 [2; 1; 3; 3; 2; 1] is [2; 1; 3; 3; 2; 1].
5. Write a parametric function replaceAll: ’a -> ’a -> ’a list -> ’a list which, given
values x and y of type 0a and an 0a list l, returns a list identical to l except that every
occurrence of x in l is replaced by y in the new list.
For example, replaceAll 2 22 [2; 1; 3; 2; 1] is [22; 1; 3; 22; 1].
6. Write a function insert:int -> int list -> int list which takes an integer n and an
integer list l sorted in strictly increasing order returns l if n is already in l; otherwise, it
“inserts” n in l while maintaining the order, that is, it returns a list that contains n and all
the elements of l and is sorted in strictly increasing order.
For example, insert 3 [1; 3; 7; 8] is [1; 3; 7; 8] and insert 5 [1; 3; 7; 8] is [1;
3; 5; 7; 8].
7. Using the function insert above, write a function sort:int list -> int list which takes
a list l and returns a list consisting of all the integer values in l in strictly increasing order.
For example, sort [1; 3; 8; 7; 1; 5; 3] is [1; 3; 5; 7; 8].
8. Write a function middle: ’a list -> ’a which takes a list with an odd number of elements
and returns its middle element.
For example, middle [4] is 4 and middle [’A’; ’B’; ’C’; ’D’, ’E’] is ’C’.
2 Programming Functional Style with Trees
Consider a variant of the binary tree datatype seen in class, parametrized by the type of element
stored in a node:
type ’a btree = Empty | Node of ’a * (’a btree) * (’a btree)
1. Write a parametric function size: ’a btree -> int which, given a binary tree t, returns
the number of (non-empty) nodes in it.
2. Write a parametric function depth: ’a btree -> int which takes as input a binary tree
t, and returns the depth of t, that is, the length of the longest path in t from the root to a
(non-empty) leaf node.