Exercise 4a      Name       ____________________   Points __/26

Document last modified: 
 

Valid answers are in bold.

  1. (5) Which are valid for definition:

    datatype intbunch = One of int
    |                             Group of int list;

    1. One 5;                           
    2. [One 5, One 4];                            
    3. Group[1,2,3];                             
    4. [Group[1,2,3], One 5];    
    5. Group[One 5, One 3]; 

     

  2. (4) Which are valid for definition: 

    datatype bunch = One of int
    |                         Group of bunch list;

    1. One 5;                    
    2. Group[Group[One 5,One 4],Group[One 2]];
    3. Group[Group[One 5,One 4],One 4];   
    4. Group[Group[One 5.4,One 4.2],One 4.7];   

                          

  3. (4) Which are valid for definition: 

    datatype 'ex bunch = One of 'ex
    |                              Group of 'ex bunch list;

    1. One 5;                    
    2. Group[Group[One 5,One 4],Group[One 2]];
    3. Group[Group[One 5,One 4],One 4];   
    4. Group[Group[One 5.4,One 4.2],One 4.7];   
  4. (5) Which are valid for definition: 
  5. datatype 'ex bunch = NIL
    |                              One of 'ex
    |                              Group of 'ex * 'ex bunch;

    1. NIL;
    2. One 5;                    
    3. Group(4,Group(5, One 6));
    4. Group(4,Group(5, NIL));
    5. Group(true,Group(false, One false));
  6. (4) Define a function to return the sum of the elements of a bunch as defined in Question 4.

    sum (Group( 3, Group( 2, One 1) )); returns 6

    fun sum NIL = 0
    |    sum (One x) = x
    |    sum (Group(h,t)) = h+sum t;