From 554ef48c6346a04fcdeb78be77b902ade75816ca Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 16:47:56 +0000 Subject: [PATCH] ocaml: phase 5.1 run_decode.ml baseline (RLE decode, expansion sum = 21) Inverse of run_length.ml from iteration 130. Takes a list of (value, count) tuples and expands: let rec rle_decode pairs = match pairs with | [] -> [] | (x, n) :: rest -> let rec rep k = if k = 0 then [] else x :: rep (k - 1) in rep n @ rle_decode rest rle_decode [(1,3); (2,2); (3,4); (1,2)] = [1;1;1; 2;2; 3;3;3;3; 1;1] sum = 3 + 4 + 12 + 2 = 21. Tests tuple-cons pattern, inner-let recursion, list concat (@), and the 'List.fold_left (+) 0' invariant on encoding round-trips. 81 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/run_decode.ml | 10 ++++++++++ plans/ocaml-on-sx.md | 6 ++++++ 3 files changed, 17 insertions(+) create mode 100644 lib/ocaml/baseline/run_decode.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 5bf23786..f21e85c2 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -66,6 +66,7 @@ "quicksort.ml": 44, "roman.ml": 44, "rpn.ml": 9, + "run_decode.ml": 21, "run_length.ml": 11, "safe_div.ml": 20, "shuffle.ml": 55, diff --git a/lib/ocaml/baseline/run_decode.ml b/lib/ocaml/baseline/run_decode.ml new file mode 100644 index 00000000..4a47e92e --- /dev/null +++ b/lib/ocaml/baseline/run_decode.ml @@ -0,0 +1,10 @@ +let rec rle_decode pairs = + match pairs with + | [] -> [] + | (x, n) :: rest -> + let rec rep k = if k = 0 then [] else x :: rep (k - 1) in + rep n @ rle_decode rest + +;; + +List.fold_left (+) 0 (rle_decode [(1, 3); (2, 2); (3, 4); (1, 2)]) diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index cb7d3e01..8a5be0a8 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,12 @@ _Newest first._ binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree * 'a tree`) with insert + in-order traversal. Tests parametric ADT, recursive match, List.append, List.fold_left. +- 2026-05-09 Phase 5.1 — run_decode.ml baseline (RLE decode, sum of + expansion = 21). Inverse of run_length.ml: takes a list of + `(value, count)` tuples, expands each pair via inner `rep` helper, + and concatenates with `@`. Companion to run_length encoding from + iteration 130. `[(1,3);(2,2);(3,4);(1,2)]` expands to + [1;1;1;2;2;3;3;3;3;1;1] (sum = 21). 81 baseline programs total. - 2026-05-09 Phase 5.1 — peano.ml baseline (Peano arithmetic, 5*6 = 30). Defines `type peano = Zero | Succ of peano` and four recursive functions: to_int, from_int, plus, mul. Multiplication