From cecde8733ac6c8b1d1b7069d980433e9bb676f97 Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 21:26:31 +0000 Subject: [PATCH] ocaml: phase 5.1 partition.ml baseline (stable partition, evens*100 + odds = 3025) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two ref lists accumulating in reverse, then List.rev'd — preserves original order: let partition pred xs = let yes = ref [] in let no = ref [] in List.iter (fun x -> if pred x then yes := x :: !yes else no := x :: !no ) xs; (List.rev !yes, List.rev !no) partition (fun x -> x mod 2 = 0) [1..10] -> ([2;4;6;8;10], [1;3;5;7;9]) evens sum * 100 + odds sum = 30 * 100 + 25 = 3025 Tests higher-order predicate, tuple return, and iter-98 let-tuple destructuring on the call site. 108 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/partition.ml | 13 +++++++++++++ plans/ocaml-on-sx.md | 7 +++++++ 3 files changed, 21 insertions(+) create mode 100644 lib/ocaml/baseline/partition.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 862e864c..95229766 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -74,6 +74,7 @@ "option_match.ml": 5, "palindrome.ml": 4, "paren_depth.ml": 7, + "partition.ml": 3025, "pancake_sort.ml": 910, "pascal.ml": 252, "peano.ml": 30, diff --git a/lib/ocaml/baseline/partition.ml b/lib/ocaml/baseline/partition.ml new file mode 100644 index 00000000..b1cf0bf6 --- /dev/null +++ b/lib/ocaml/baseline/partition.ml @@ -0,0 +1,13 @@ +let partition pred xs = + let yes = ref [] in + let no = ref [] in + List.iter (fun x -> + if pred x then yes := x :: !yes + else no := x :: !no + ) xs; + (List.rev !yes, List.rev !no) + +;; + +let (evens, odds) = partition (fun x -> x mod 2 = 0) [1;2;3;4;5;6;7;8;9;10] in +List.fold_left (+) 0 evens * 100 + List.fold_left (+) 0 odds diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 56749bed..5b89f459 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,13 @@ _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 — partition.ml baseline (stable partition by + predicate, 30*100 + 25 = 3025). Two ref lists accumulating in + reverse, then List.rev'd — preserves original order. Test: + `partition (fun x -> x mod 2 = 0) [1..10]` → ([2;4;6;8;10], + [1;3;5;7;9]) → 30*100 + 25 = 3025. Tests higher-order predicate + + tuple return + iter-98 let-tuple destructuring. 108 baseline + programs total. - 2026-05-09 Phase 5.1 — is_prime.ml baseline (count primes ≤ 100 = 25). Trial division up to √n with early-exit via bool ref. Loop 2..n calling is_prime, accumulate count. Returns 25 (the canonical