From fad81e0b0c89896f2aa222081ecadbadbd243649 Mon Sep 17 00:00:00 2001 From: giles Date: Mon, 11 May 2026 05:25:03 +0000 Subject: [PATCH] ocaml: phase 5.1 combinations.ml baseline (C(9, 4) = 126) Pascal-recursion combination enumerator: let rec choose k xs = if k = 0 then [[]] else match xs with | [] -> [] | h :: rest -> List.map (fun c -> h :: c) (choose (k - 1) rest) @ choose k rest C(9, 4) = |choose 4 [1; ...; 9]| = 126 Tests pure-functional enumeration with List.map + closure over h, @ append, [] | h :: rest pattern match on shrinking input. 200 baseline programs total -- milestone. --- lib/ocaml/baseline/combinations.ml | 12 ++++++++++++ lib/ocaml/baseline/expected.json | 1 + plans/ocaml-on-sx.md | 8 ++++++++ 3 files changed, 21 insertions(+) create mode 100644 lib/ocaml/baseline/combinations.ml diff --git a/lib/ocaml/baseline/combinations.ml b/lib/ocaml/baseline/combinations.ml new file mode 100644 index 00000000..25a096d9 --- /dev/null +++ b/lib/ocaml/baseline/combinations.ml @@ -0,0 +1,12 @@ +let rec choose k xs = + if k = 0 then [[]] + else + match xs with + | [] -> [] + | h :: rest -> + List.map (fun c -> h :: c) (choose (k - 1) rest) + @ choose k rest + +;; + +List.length (choose 4 [1; 2; 3; 4; 5; 6; 7; 8; 9]) diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 025653bd..ed41cf21 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -29,6 +29,7 @@ "calc.ml": 13, "catalan.ml": 42, "closures.ml": 315, + "combinations.ml": 126, "convex_hull.ml": 5, "coin_change.ml": 6, "coin_min.ml": 6, diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 50b4dedc..5a5fe298 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-11 Phase 5.1 — combinations.ml baseline (C(9, 4) = 126 + enumerated). Pascal-style recursive split: with first element h, + combinations either include h (recurse with k−1 on rest) or + exclude h (recurse with k on rest); identity choose k [] = []. + C(9, 4) = 126 = 9!/(4!·5!). Tests pure-functional combination + enumeration with `List.map` + closure over `h`, `@` append, + and `[] | h :: rest` pattern match on shrinking input. + 200 baseline programs total. - 2026-05-11 Phase 5.1 — unique_paths_obs.ml baseline (count monotone paths in 4×4 grid with obstacles at (1,1),(2,3),(3,0) = 3). Standard 2D DP with obstacle gating: dp[i][j] = dp[i-1][j]