Exercise 3      Name       ____________________   Points __/17

Document last modified: 
 

  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")].


           
    filter (gt4, [(7,"CSCI"),(3,"CHEM"), (6,"PHY")] );
     
  2. What is the Cartesian product of:

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

    A x B = { (1, "a"), (1, "b"), (1, "c"), (2, "a"), (2, "b"), (2, "c"), (3, "a"), (3, "b"), (3, "c")}
     
  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")]


    fun dist _ []     = []
    |   dist a (h::t) = (a,h)::(dist a t);

     
  4. 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.

        fun pairs [] _     = []
      |   pairs (h::t) L = (dist h L)@(pairs t L);

     
  5. Given member and union functions what is:

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

            [(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.

      datatype coin = dollar | half | quarter | dime | nickel | penny;
       
    2. The union SIR of a string, int or real.

      datatype SIR = S of string
      |              I of int
      |              R of real;

       
    3. Add coin to the union SIR to define the union SIRC.

      datatype SIR = S of string
      |              I of int
      |              R of real
      |              C of coin;

       
  7. (3) Define function amount that totals the decimal amount of a list of coin. For example:

        amount [dollar, dollar, dime, nickel]; returns 215

       
        fun amount []           = 0
        |   amount (dollar::t)  = 100+amount t
        |   amount (half::t)    = 50 +amount t
        |   amount (quarter::t) = 25 +amount t
        |   amount (dime::t)    = 10 +amount t
        |   amount (nickel::t)  = 5 +amount t
        |   amount (penny::t)   = 1 +amount t;

     
  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 ((I h)::t)         = h + amount t
    |   amount ((S "dollar")::t)  = 100+amount t
    |   amount ((S "nickel")::t)  = 5 +amount t
    |   amount ((C dollar)::t)    = 100 +amount t
    |   amount ((C half)::t)      = 50 +amount t
    |   amount ((C quarter)::t)   = 25 +amount t
    |   amount ((C dime)::t)      = 10 +amount t
    |   amount ((C nickel)::t)    = 5 +amount t
    |   amount ((C penny)::t)     = 1 +amount t;