## ML Types: Practice sheet

Determine the types of the following ML types and objects, or state that they are not type correct. For the functions, be sure you understand how they work. This is NOT TO HAND IN, as the assignment can be done trivially by typing each of them into the ML interpreter.
• ([2,3],[4,5,5]);
• [(2,3), (4,5)];
• ([2,3],[4.1,5.2,5.3]);
• [(2.1,3), (4.2,5)];
• [(2.1,3), (4,5.2)];
• [([2],3), ([4,5],6)];
• ([(2),3], [(4,5),6]);
• ([(2),3], [4,6]);
• fun min(X,Y) = if X < Y then X else Y;
• fun safeDivide(X,Y) = if Y = 0 then false else X div Y;
(Note: X div Y is integer divide).
• fun pairToList(X,Y) = [X,Y];
• fun pairToPairOfLists(X,Y) = ([X],[Y]);
• fun repeat2a(f,x) = f(f(x));
• fun repeat2(f) = fn(x) => f(f(x));
• fun repeatN(f,n) = fn(x) => if (n=0) then x else f(repeatN(f,n-1)(x));
That is repeatN(f,1)(x) = f(x). repeatN(f,2)(x) = f(f(x)). repeatN(f,n)(x) = f(f(f ... n times (f(x)) ...))
• repeatN(fn(x) => x+3,4);
• fun repeat f n x = if n=0 then x else f(repeat f (n-1) x);
(Curried version of repeatN.)
• repeat tl;
• repeat tl 3;
• repeat tl 3 [10,20,30,40];
• ``` fun f [X] = [X] | f(X::Y) = g(X::Y)::f(Y)
and
g(X::Y) = hd(f(Y));
```