ocaml: phase 5.1 run_length.ml baseline (RLE, sum-of-counts = 11)
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.
This commit is contained in:
2026-05-09 09:47:25 +00:00
parent 3be2dc6e78
commit 32aba1823d
3 changed files with 24 additions and 0 deletions

View File

@@ -36,6 +36,7 @@
"quicksort.ml": 44,
"roman.ml": 44,
"rpn.ml": 9,
"run_length.ml": 11,
"safe_div.ml": 20,
"shuffle.ml": 55,
"word_freq.ml": 8,

View File

@@ -0,0 +1,16 @@
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])