Exercise 3      Name       ____________________   Points __/17

Document last modified: 
 

  1. (1) Below is the gt4 function that returns true for tuples where value of first element is greater than 4.

        fun gt4 (x, _) = x > 4;

    Use gt4 and the filter functional to return only tuples where the first element is greater than 4. Filtering [(7,"CSCI"),(3,"CHEM"), (6,"PHY")] returns [(7, "CSCI"), (6, "PHY")].


     
  2. (1) What is the Cartesian product of:

    A = {1, 2, 3};
    B = {"a", "b", "c"}

    A x B =
     
  3. (3) Write a function named dist to construct a list of tuples from one element paired with every element of a list. For example:

    dist (3, ["a","b","c"]) returns [(3, "a"), (3, "b"), (3, "c")]
     
  4. (3) Write a function named pairs that constructs all possible tuples of two lists. For example:

    pairs [1,2,3,4] ["a","b","c","d"]; returns
        [(1, "a"), (1, "b"), (1, "c"), (1, "d"),
         (2, "a"), (2, "b"), (2, "c"), (2, "d"),
         (3, "a"), (3, "b"), (3, "c"), (3, "d"),
         (4, "a"), (4, "b"), (4, "c"), (4, "d")]

    Hint: Use dist.
     
  5. (1) Given member and union functions what is:

    union ( [(1, "a"), (1, "b"), (1, "c")], [(1, "a"), (1, "x"), (2, "c")]);

    fun member (_,[]) = false
    |    member (a,(h::t)) =
                        if a=h then true
                        else member (a, t);

    fun union ([],L) = L
    |    union ((h::t),L) =
                    if member(h,L) then union(t,L)
                    else h::union(t,L);

  6. (3) Give appropriate ML datatype definitions for the following:
     
    1. Enumerate the set coin using the names of US coins worth $1.00 or less.
    2. The union SIR of a string, int or real.
    3. Add coin to the union SIR to define the union SIRC.
       
  7. (3) Define function amount that totals the decimal amount of a list of coin. For example:

        amount [dollar, dollar, dime, nickel]; returns 215
     
  8. (2) Complete function amount that totals the decimal amount of a list of SIRC. For example:
       
        amount [I 43, C dollar, R 52.0, S "nickel" ]; returns 200


        fun amount [] = 0
        | amount ((R h)::t) = (trunc h) + amount t
        | amount ((S "dollar")::t) = 100 + amount t
        | amount ((C dollar)::t) = 100+amount t;