diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 904c4111..b2a4af71 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -16,6 +16,7 @@ "lambda_calc.ml": 7, "levenshtein.ml": 11, "memo_fib.ml": 75025, + "merge_sort.ml": 44, "module_use.ml": 3, "mutable_record.ml": 10, "option_match.ml": 5, diff --git a/lib/ocaml/baseline/merge_sort.ml b/lib/ocaml/baseline/merge_sort.ml new file mode 100644 index 00000000..cfcc30d6 --- /dev/null +++ b/lib/ocaml/baseline/merge_sort.ml @@ -0,0 +1,28 @@ +let rec split lst = + match lst with + | [] -> ([], []) + | [x] -> ([x], []) + | x :: y :: rest -> + let (a, b) = split rest in + (x :: a, y :: b) + +let rec merge xs ys = + match xs with + | [] -> ys + | x :: xs' -> + match ys with + | [] -> xs + | y :: ys' -> + if x <= y then x :: merge xs' (y :: ys') + else y :: merge (x :: xs') ys' + +let rec sort lst = + match lst with + | [] -> [] + | [x] -> [x] + | _ -> + let (a, b) = split lst in + merge (sort a) (sort b) +;; + +List.fold_left (+) 0 (sort [3;1;4;1;5;9;2;6;5;3;5]) diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 3ee58e54..17e0c98c 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 — merge_sort.ml baseline (user-implemented + mergesort, sorted sum = 44). Stress-tests `let (a, b) = split rest + in (x :: a, y :: b)` (let-tuple destructuring inside a recursive + match arm), nested match-in-match for the merge merge step, and + the (op) operator section `(+)` as fold accumulator. 26 baseline + programs total. - 2026-05-09 Phase 4 — top-level `let f (a, b) = body` tuple-param decl (+3 tests, 559 total). parse-decl-let (which lives outside the ocaml-parse scope and lacks parse-pattern access) uses a