| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
type 'a t = |
| 12 |
| Nil |
| 13 |
| Cons of 'a * 'a t |
| 14 |
| Default of int * 'a t |
| 15 |
|
| 16 |
|
| 17 |
let empty = Nil |
| 18 |
let cons x l = Cons (x, l) |
| 19 |
let defaultn n l = |
| 20 |
if Int.equal n 0 then l |
| 21 |
else match l with |
| 22 |
| Nil | Cons _ -> Default (n, l) |
| 23 |
| Default (m, l) -> Default (n + m, l) |
| 24 |
|
| 25 |
let default l = match l with |
| 26 |
| Nil | Cons _ -> Default (1, l) |
| 27 |
| Default (m, l) -> Default (m + 1, l) |
| 28 |
|
| 29 |
let cons_opt o l = match o with |
| 30 |
| None -> default l |
| 31 |
| Some x -> cons x l |
| 32 |
|
| 33 |
let is_empty = function |
| 34 |
| Nil -> true |
| 35 |
| Cons _ | Default _ -> false |
| 36 |
|
| 37 |
let is_default = function |
| 38 |
| Nil -> true |
| 39 |
| Default (_, Nil) -> true |
| 40 |
| Cons _ | Default _ -> false |
| 41 |
|
| 42 |
let view = function |
| 43 |
| Nil -> None |
| 44 |
| Cons (x, l) -> Some (Some x, l) |
| 45 |
| Default (1, l) -> Some (None, l) |
| 46 |
| Default (n, l) -> Some (None, Default (n - 1, l)) |
| 47 |
|
| 48 |
let rec to_list l = match l with |
| 49 |
| Nil -> [] |
| 50 |
| Cons (x, l) -> Some x :: to_list l |
| 51 |
| Default (n, l) -> |
| 52 |
let l = to_list l in |
| 53 |
let rec iterate n l = if n <= 0 then l else iterate (n - 1) (None :: l) in |
| 54 |
iterate n l |
| 55 |
|
| 56 |
let of_full_list l = List.fold_right cons l empty |
| 57 |
|
| 58 |
let equal eq l1 l2 = |
| 59 |
let eq o1 o2 = match o1, o2 with |
| 60 |
| None, None -> true |
| 61 |
| Some x1, Some x2 -> eq x1 x2 |
| 62 |
| Some _, None | None, Some _ -> false |
| 63 |
in |
| 64 |
CList.for_all2eq eq (to_list l1) (to_list l2) |
| 65 |
|
| 66 |
let compare cmp l1 l2 = CList.compare (Option.compare cmp) (to_list l1) (to_list l2) |
| 67 |
|
| 68 |
let length l = |
| 69 |
let rec length n = function |
| 70 |
| Nil -> n |
| 71 |
| Cons (_, l) -> length (n + 1) l |
| 72 |
| Default (k, l) -> length (k + n) l |
| 73 |
in |
| 74 |
length 0 l |
| 75 |
|
| 76 |
module Skip = |
| 77 |
struct |
| 78 |
|
| 79 |
let rec iter f = function |
| 80 |
| Nil -> () |
| 81 |
| Cons (x, l) -> |
| 82 |
let () = f x in |
| 83 |
iter f l |
| 84 |
| Default (_, l) -> iter f l |
| 85 |
|
| 86 |
let rec map f = function |
| 87 |
| Nil -> Nil |
| 88 |
| Cons (x, l) -> Cons (f x, map f l) |
| 89 |
| Default (n, l) -> Default (n, map f l) |
| 90 |
|
| 91 |
let rec fold f accu = function |
| 92 |
| Nil -> accu |
| 93 |
| Cons (x, l) -> fold f (f accu x) l |
| 94 |
| Default (_, l) -> fold f accu l |
| 95 |
|
| 96 |
let rec for_all f l = match l with |
| 97 |
| Nil -> true |
| 98 |
| Cons (x, l) -> f x && for_all f l |
| 99 |
| Default (_, l) -> for_all f l |
| 100 |
|
| 101 |
let rec exists f l = match l with |
| 102 |
| Nil -> false |
| 103 |
| Cons (x, l) -> f x || exists f l |
| 104 |
| Default (_, l) -> exists f l |
| 105 |
|
| 106 |
end |
| 107 |
|
| 108 |
module Smart = |
| 109 |
struct |
| 110 |
|
| 111 |
let rec map f l = match l with |
| 112 |
| Nil -> empty |
| 113 |
| Cons (x, r) -> |
| 114 |
let x' = f x in |
| 115 |
let r' = map f r in |
| 116 |
if x' == x && r' == r then l else cons x' r' |
| 117 |
| Default (n, r) -> |
| 118 |
let r' = map f r in |
| 119 |
if r' == r then l else Default (n, r') |
| 120 |
|
| 121 |
let rec fold_left_map f accu l0 = match l0 with |
| 122 |
| Nil -> accu, empty |
| 123 |
| Cons (x, l) -> |
| 124 |
let accu, x' = f accu x in |
| 125 |
let accu, l' = fold_left_map f accu l in |
| 126 |
let r = if x' == x && l' == l then l0 else Cons (x', l') in |
| 127 |
accu, r |
| 128 |
| Default (n, l) -> |
| 129 |
let accu, l' = fold_left_map f accu l in |
| 130 |
let r = if l' == l then l0 else Default (n, l') in |
| 131 |
accu, r |
| 132 |
|
| 133 |
end |