Test 1            C311 Programming Languages

True or False

  1. _F__    ML generally performs type coercions implicitly.
  2. _T__    Tuples are heterogeneous, tuples can contain other tuples (i.e. mixed types).
  3. _F__    [[1, 4],[ ~1,5], [3, 4], 5] is a valid ML list.
  4. _T__    fun f x y = x * y; is curried.
  5. _T__    A function with no functions as a parameter and does not return a function is order 1.
  6. _F__    (int -> int) * (int -> int) -> (int -> int) is order 3.
  7. _T__    reduce is the same as foldr.
  8. _T__    foldl is the same as foldr for associative binary operations such as + and *.

Matching (not exhaustive)

  1. _b__    fn a => fn b => a * b;   
  2. _c__    fn b => 4 * b; 
  3. _e__    12 
  4. _NA_    8
  5. _d__    9
  1. fun f (a,b) = a * b;
  2. fun f a b = a * b;    f;
  3. fun f a b = a * b;    f  4;
  4. reduce (fn a => fn b => a + b) 1 [3,1,4];
  5. (fn a => fn b => b div a) 2 24;

Short Answer - Give result using the following definitions, some may be an error.

 fun map _ [ ] = [ ] 
 |     map f (h::t) = (f h)::(map f  t);
 fun filter _ [ ] = [ ]
 |     filter p (h::t) = 
             if (p h) then h::(filter p t)
             else filter p t;
 fun reduce _ a [ ] = a
 |     reduce F a (h::t) = F h (reduce F a t); 
 fun map2 _ [ ] [ ] = [ ]
 |   map2 F (h1::t1) (h2::t2) = (F h1 h2) :: (map2 F t1 t2); 
datatype IS = I of int | S of string;  fun m (I x) (I y) = I (x * y)
 |    m (I x) (S y) = (S y);
 fun g x y = (x, y);  fun h x y = y@[x];
  1. g 2 "b";                                        ___(2,"b")_____

  2. h [3, 2] 1                                     ____Fails    ____

  3. map (fn a => a + 1) [1,2,3,4];        ___[2,3,4,5] ___

  4. map2 g [1,2] ["a","b"];                   _[(1,"a"), (2,"b")]

  5. reduce h [ ] [1,2,3];                       ___[3,2,1] _____

  6. filter (fn x => x > 3) [1,2,3,4,5];     ___[4,5]    _____

  7. m (I 5) (S "a");                             _____S "a"______

  8. m (I 5) (I 6);                                _____I 30_______

  9. m (S "a") (I 5);                             _____Fails  _____

  10. Domain of g                                 __polymorphic___

  11. Range of g                                   _polymorphic tuples_

Programming in ML

  1. Define the function count5 to count the number of 5's in a list, for example:

        count5 [] is 0
        count5 [1,5,2,5,3,5] is 3

        fun count5 [] = 0
        |    count5 (h::t) = if h=5 then 1+count5 t else count5 t;

  2. Given a union data type definition of:

        datatype IRS = I of int | R of real | S of string | IR of (int * real) | IS of (int * string);

    Define the Imult function that multiples two I IRS types and returns an I IRS type. Ignore the others. For example:

        Imult (I 5) (I 4); is (I 20)

        fun Imult (I x) (I y) = I(x*y);

  3. Using the datatype definition of Question 26, define the IxR function that given a I IRS type and a R IRS type returns an IR IRS type. For example:

        IxR (I 4) (R 5.2); returns (IR (4,5.2) )

        fun IxR (I i) (R r) = (IR (i, r));

  4. Given the datatype definition of:

        datatype 'element mylist = NIL | CONS of 'element * 'element mylist;

    Define the function to count the number of 'elements. For example:

        count CONS(8,CONS(2,CONS(6, NIL))); is 3


        fun count NIL = 0
        |    count (CONS(h,t)) = 1 + count t;