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
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:
@@ -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,
|
||||
|
||||
16
lib/ocaml/baseline/run_length.ml
Normal file
16
lib/ocaml/baseline/run_length.ml
Normal 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])
|
||||
Reference in New Issue
Block a user