csci2041/public-class-repo/SamplePrograms/Sec_10_3:35pm/fold.ml

48 lines
811 B
OCaml
Raw Normal View History

2018-01-29 23:35:31 +00:00
(* Sec 01, Feb 3 *)
(* fold (+) 0 [1;2;3;4]
fold: ( 'a -> 'b -> 'a )
-> 'a
-> 'a list
-> 'b
We can see this as
1 + (2 + (3 + (4 + 0)))
*)
let rec fold_v1 f i l =
match l with
| [] -> i
| x::rest -> f x (fold_v1 f i rest)
let string_folder x s = string_of_int x ^ " " ^ s
(* fold (+) 0 [1;2;3;4]
as
((((0 + 1) + 2) + 3) + 4)
*)
let rec fold_v2 f i l =
match l with
| [] -> i
| x::rest -> fold_v2 f (f i x) rest
let rec foldr (f:'a -> 'b -> 'b) (l:'a list) (v:'b) : 'b =
match l with
| [] -> v
| x::xs -> f x (foldr f xs v)
(* foldl (+) 0 [1;2;3;4]
as
((((0 + 1) + 2) + 3) + 4) *)
let rec foldl f v l =
match l with
| [] -> v
| x::xs -> foldl f (f v x) xs
let length lst = foldl (fun n x -> n + 1 ) 0 lst
let sum lst = foldl (+) 0 lst