Define a function which computes the product of all integers between
m and n (with n >= m) inclusive.
Use this function to define the function Cn,k (the
number of combinations
of n elements taken k by k), which is defined by
Cn,k = n!/(k!*(n-k)!)
Define a function
power : int * int -> int
so that, for m >=0
power(n,m) = nm
holds. Note: we assume that 00 is defined as 1.
The positive integer square root of a non-negative integer is a
function introot such that: if introot m = n,
then n is the largest
integer such that n2 is less than or equal to m.
Define the function introot in ML.
In ML, like in any other language, the if-then-else construct is
non-strict, i.e. only one of the branches is evaluated,
depending on the the result of the test.
What would be the consequences for recursive definitions if the if-then-else
were strict (meaning that first both the branches are evaluated,
and then one of the results is picked, depending on the
result of the test), and pattern-matching were not available?
Define a function copy: int * 'a -> 'a list
such that copy(k,x) gives the list containing k occurrences
of x. Examples:
copy(0,5) = 
copy(1,5) = 
copy(3,"a") = ["a","a","a"]
copy(3,copy(1,8)) = [,,]
Define a function sumlists: int list * int list -> int list
which takes in input two lists of integers and gives as result the list of the
sums of the elements in corresponding position in the input lists.
The shortest list has to be seen as extended with 0's.
sumlists(,) = 
sumlists([1,2],[3,4]) = [4,6]
sumlists(,[3,4,2]) = [4,4,2]
sumlists([1,6],) = [4,6]
Define a function remove_dup: ''a list -> ''a list
which takes in input a list and removes all the duplicates,
remove_dup  = 
remove_dup [1,2,1] = [1,2]
remove_dup ["a","a","a"] = ["a"]
remove_dup [,[1,2],[1,2,3],[1,2],[4,5]] = [,[1,2],[1,2,3],[4,5]]
Is it possible to define remove_dup with a more generl type,
i.e. remove_dup: 'a list -> 'a list?
Define a function first_list: ('a * 'b) list -> 'a list
which takes in input a list of pairs and gives back the list consisting of
the first elements only,
first_list  = 
first_list [(1,2),(1,3)] = [1,1]
first_list [(1,"a"),(2,"b"),(3,"c")] = [1,2,3]
first_list [(,"a"),(,"b"),([1,2],"c")] = [,,[1,2]]
Define a function flatten: 'a list list -> 'a list
which takes in input a list of lists and gives back the list consisting of
all the elements, in the same order in which they appear in the argument.
flatten [] = 
flatten [[1,2],[2,3,4],,,[6,7]] = [1,2,2,3,4,5,6,7]
flatten [["a"],["b","a"]] = ["a","b","a"]
Consider the data structure "binary tree",
defined in Assignment 7:
datatype 'a btree = emptybt | consbt of 'a * 'a btree * 'a btree;
Define a function sum_tree : int btree -> int
which returns the sum of all the elements of the tree (0 if the
tree is empty)
Consider the data structure "binary tree" of previous exercise.
Define a function mirror : 'a btree -> 'a btree
which returns the "mirror" of the input tree. Example:
/ \ / \
2 3 - mirror -> 3 2
/ \ / \
4 5 5 4
Define a function breadth_first: 'a btree -> 'a list
which takes a binary tree
and returns a list representing the breadth first visit of the tree
(visit by level).
/ \ \
4 5 6 - breadth_first -> [1,2,3,4,5,6,7,8,9]
Define a data structure 'a tree, which
has an arbitrary number of subtrees for every node.
On this structure, define the function pre_visit.
4 5 6 7_ - pre_visit -> [1,2,4,5,6,3,7,8,9,0,9]
8 9 0 9
We want to organize a data base containing information
about a number of people in such a way that it is easy to perform a search.
To such purpose, we want to use a search three. For each person
we will have a record containing the following information: