1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
type 'a t =
| Nil
| Cons of 'a * (unit -> 'a t)
let one e =
Cons (e, fun () -> Nil)
let rec append ll1 f_ll2 =
match ll1 with
| Cons (e, f_ll1) -> Cons (e, fun () -> append (f_ll1 ()) f_ll2)
| Nil -> f_ll2 ()
let rec append_mix ll1 f_ll2 =
match ll1 with
| Cons (e, f_ll1) -> Cons (e, fun () -> append_mix (f_ll2 ()) f_ll1)
| Nil -> f_ll2 ()
let rec map f ll =
match ll with
| Cons (e, f_ll) -> Cons (f e, fun () -> map f (f_ll ()))
| Nil -> Nil
let mapi f ll =
let rec mapi_aux ll i =
match ll with
| Cons (e, f_ll) -> Cons (f e i, fun () -> mapi_aux (f_ll ()) (i + 1))
| Nil -> Nil in
mapi_aux ll 0
let rec iter f ll =
match ll with
| Cons (e, f_ll) -> f e; iter f (f_ll ())
| Nil -> ()
let rec filter_map f ll =
match ll with
| Cons (e, f_ll) ->
(match f e with
| Some e -> Cons (e, fun () -> filter_map f (f_ll ()))
| None -> filter_map f (f_ll ()))
| Nil -> Nil
let rec fold_left f acc ll =
match ll with
| Cons (e, f_ll) -> fold_left f (f acc e) (f_ll ())
| Nil -> acc
let rec fold_right f ll acc =
match ll with
| Cons (e, f_ll) -> f e (fold_right f (f_ll ()) acc)
| Nil -> acc
let rec join ll_ll =
match ll_ll with
| Cons (ll, f_ll_ll) -> append ll (fun () -> join (f_ll_ll ()))
| Nil -> Nil
let rec join_mix ll_ll =
match ll_ll with
| Cons (ll, f_ll_ll) -> append_mix ll (fun () -> join_mix (f_ll_ll ()))
| Nil -> Nil
let rec bind_mix f ll =
match ll with
| Cons (e, f_ll) -> append_mix (f e) (fun () -> bind_mix f (f_ll ()))
| Nil -> Nil
let rec from_list l =
match l with
| e :: l -> Cons (e, fun () -> from_list l)
| [] -> Nil