- Lambda Calculus
- 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
-
- 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
-
- 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
-
- Examples
- max()
- isMember()
- union()
- intersect()
- SML Exercises
- Write a function
il2rl
that converts an int list to a real list.
- Write a function
dupList
that takes [1, 2, 3] and produces [1, 1, 2, 2, 3, 3].
- Define a function
mymap
with the same type and behavior as map
.
- Define a function
myfoldr
with the same type and behavior as foldr
.
- Define a function
myfoldl
with the same type and behavior as foldl
.