Lab 3: Lambda Calculus and Currying functions (map, foldr, foldl)

  1. Lambda Calculus
  2. map
    - map;
    val it = fn : ('a -> 'b) -> 'a list -> 'b list
    - map ~ [1, 2, 3, 4];
    val it = [~1,~2,~3,~4] : int list
    - map not [false, true];
    val it = [true,false] : bool list
    - map (fn x => x*x) [1, 2, 3, 4];
    val it = [1,4,9,16] : int list
    - map (fn x => print (x ^ "\n")) ["1", "2", "3", "4"];
    1
    2
    3
    4
    val it = [(),(),(),()] : unit list
    - 
      
  3. foldr
    - foldr;
    val it = fn : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b
    - foldr (fn(a, b) => a - b) 0 [1, 2, 3, 4];
    val it = ~2 : int
    - fun sub(a, b) = a - b;
    val sub = fn : int * int -> int
    - sub(1, sub(2, sub(3, sub(4, 0))));
    val it = ~2 : int
    - foldr (op -) 0 [1, 2, 3, 4];
    val it = ~2 : int
    - 
      
  4. foldl
    - foldl;
    val it = fn : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b
    - foldl (fn(a, b) => a - b) 0 [1, 2, 3, 4];
    val it = 2 : int
    - fun sub(a, b) = a - b;
    val sub = fn : int * int -> int
    - sub(4, sub(3, sub(2, sub(1, 0))));
    val it = 2 : int
    - foldl (op -) 0 [1, 2, 3, 4];
    val it = 2 : int
    - 
      
  5. Examples
    1. max()
    2. isMember()
    3. union()
    4. intersect()
  6. SML Exercises
    1. Write a function il2rl that converts an int list to a real list.
    2. Write a function dupList that takes [1, 2, 3] and produces [1, 1, 2, 2, 3, 3].
    3. Define a function mymap with the same type and behavior as map.
    4. Define a function myfoldr with the same type and behavior as foldr.
    5. Define a function myfoldl with the same type and behavior as foldl.