Exercise 4      Name       ____________________   Points __/18

Document last modified: 
 

  1. (5) Give the anonymous functions equivalent to:
    1. fun dubal x = 2 * x;                           
    2. fun sub x y = x - y;                            
    3. fun gt x y = x > y;                             
    4. fun max x y = if x > y then x else y;    
    5. fun pre x y z = if (x y z) then y else z; 
  2. (4) Duplicate using anonymous functions.
    1. map dubal [1,2,3,4];                    
    2. foldr sub 1 [1,2,3,4];                 
    3. map2 sub [1,2,3] [4,5,6]              
    4. map2 max [3,2,1] [1,2,3];            
  3. (2) Give the staged version of the member function.

    fun member (_,[]) = false                              
    |    member (a,(h::t)) =                                  
                        if a=h then true                          
                        else member (a, t);                     
     
  4. (7) Evaluate the following:
    1. foldr (op *) 1 [1,2,3,4];                            
    2. foldr (op -) 0 [1,2,3,4];                             
    3. map (gt 2) [1,2,3,4];                                
    4. foldl (op -) 0 [1,2,3,4];                             
    5. map (max 2) [1,2,3,4];                             
    6. map ((fn x => fn y => x > y) 2) [1,2,3,4]; 
    7. [dubal 4, sub 5 4];