Exercise 2          Name _____________________  Pts ___/20

Translate into patterns or if-then statements:  
1. fun insert a [] = [a]
|    insert a (h::t) = if a<h then a::h::t else h::insert a t;
 
2. fun f2 L = if null L then []
                     else if null (tl L) then L
                             else [hd (tl L)];
 
3. fun map(f,L) = if null L then []                     
                         else f (hd L)::map(f, (tl L));
 
4. fun filter f L = if null L then []
                       else if f (hd L) then hd L::filter f (tl L)
                              else filter f (tl L);
 
5. fun reduce (_,a,[]) = a
|    reduce(f,a,h::t) = f h (reduce(f,a,t));
 

Determine the result given the above and following definitions, indicate those in error.

fun min a b = if a<b then a else b;

fun prime n =
  let
      fun remainder 0 = true
      |    remainder 1 = true
      |    remainder m = if (n mod m) = 0 then false else remainder(m-1)
  in
      remainder (n-1)
 end;

6. reduce( insert, [], [5,3,1,7]);  
7. reduce( min, 9999999, [5,3,1,7]);  
8. prime 9;  
9. filter prime [17, 11, 12, 3, 6];              
10. insert(1,[2,3]);