From a98d683e60d255aea1772e6f6d2c129b51b6b0f4 Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 14:40:22 +0000 Subject: [PATCH] ocaml: phase 5.1 group_consec.ml baseline (group consecutive equals, 5*10+3 = 53) Inner 'collect cur acc tail' walks the tail while head matches 'cur', accumulating into 'acc'. Returns (rev acc, remaining) on first mismatch. Outer 'group' recurses on the remaining list. group [1;1;2;2;2;3;1;1;4] = [[1;1]; [2;2;2]; [3]; [1;1]; [4]] List.length groups = 5 List.length (gs.(1)) = 3 5 * 10 + 3 = 53 Tests nested recursion (inner aux + outer recursion), tuple destructuring 'let (g, tail) = ...' inside the outer match arm, and List.nth. 69 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/group_consec.ml | 18 ++++++++++++++++++ plans/ocaml-on-sx.md | 8 ++++++++ 3 files changed, 27 insertions(+) create mode 100644 lib/ocaml/baseline/group_consec.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 208228be..019643c9 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -28,6 +28,7 @@ "frequency.ml": 5, "gcd_lcm.ml": 60, "grep_count.ml": 3, + "group_consec.ml": 53, "hailstone.ml": 111, "hanoi.ml": 1023, "fizzbuzz.ml": 57, diff --git a/lib/ocaml/baseline/group_consec.ml b/lib/ocaml/baseline/group_consec.ml new file mode 100644 index 00000000..690f0271 --- /dev/null +++ b/lib/ocaml/baseline/group_consec.ml @@ -0,0 +1,18 @@ +let rec group xs = + match xs with + | [] -> [] + | x :: rest -> + let rec collect cur acc tail = + match tail with + | [] -> (List.rev acc, []) + | y :: ys -> + if y = cur then collect cur (y :: acc) ys + else (List.rev acc, y :: ys) + in + let (g, tail) = collect x [x] rest in + g :: group tail + +;; + +let gs = group [1;1;2;2;2;3;1;1;4] in +List.length gs * 10 + List.length (List.nth gs 1) diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 09bcf0a2..c36c914a 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,14 @@ _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 — group_consec.ml baseline (group consecutive + equals into sublists, 5*10 + 3 = 53). Inner `collect cur acc + tail` walks while head matches `cur`, accumulates into `acc`, + returns `(rev acc, remaining)` on first mismatch. Outer `group` + recurses on the remaining list. [1;1;2;2;2;3;1;1;4] → + [[1;1];[2;2;2];[3];[1;1];[4]] (5 groups, second group has 3 + elements). Sum = 53. Tests nested recursion + tuple destructuring + in let-binding. 69 baseline programs total. - 2026-05-09 Phase 5.1 — zigzag.ml baseline (interleave two lists, sum 1..10 = 55). One-liner that swaps the lists on every recursive call: `match xs with [] -> ys | x :: xs' -> x :: zigzag ys xs'`.