Higher Order
# map
# fold_left and fold_right
- They combine list elements in opposite orders, as indicated by their names. Function
fold_rightcombines from the right to the left, whereasfold_leftproceeds from the left to the right. - Function
fold_leftis tail recursive, whereasfold_rightis not. - The types of the functions are different.
let rec fold_left f accu l =
match l with
[] -> accu
| a::l -> fold_left f (f accu a) l
let rec fold_right f l accu =
match l with
[] -> accu
| a::l -> f a (fold_right f l accu)
- : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a = <fun>
- : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b = <fun>