Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 29s
Run-length encoding via tail-recursive 4-arg accumulator:
let rle xs =
let rec aux xs cur n acc =
match xs with
| [] -> List.rev ((cur, n) :: acc)
| h :: t ->
if h = cur then aux t cur (n + 1) acc
else aux t h 1 ((cur, n) :: acc)
in
match xs with
| [] -> []
| h :: t -> aux t h 1 []
rle [1;1;1;2;2;3;3;3;3;1;1] = [(1,3);(2,2);(3,4);(1,2)]
sum of counts = 11 (matches input length)
The sum-of-counts test verifies that the encoding preserves total
length — drops or duplicates would diverge.
44 baseline programs total.
17 lines
341 B
OCaml
17 lines
341 B
OCaml
let rle xs =
|
|
let rec aux xs cur n acc =
|
|
match xs with
|
|
| [] -> List.rev ((cur, n) :: acc)
|
|
| h :: t ->
|
|
if h = cur then aux t cur (n + 1) acc
|
|
else aux t h 1 ((cur, n) :: acc)
|
|
in
|
|
match xs with
|
|
| [] -> []
|
|
| h :: t -> aux t h 1 []
|
|
|
|
;;
|
|
|
|
List.fold_left (fun acc (_, n) -> acc + n) 0
|
|
(rle [1;1;1;2;2;3;3;3;3;1;1])
|