ocaml: phase 5.1 merge_sort.ml baseline (user mergesort, sum=44)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s

User-implemented mergesort that exercises features added across the
last few iterations:

  let rec split lst = match lst with
    | x :: y :: rest ->
      let (a, b) = split rest in       (* iter 98 let-tuple destruct *)
      (x :: a, y :: b)
    | ...

  let rec merge xs ys = match xs with
    | x :: xs' ->
      match ys with                     (* nested match-in-match *)
      | y :: ys' -> ...

  ...

  List.fold_left (+) 0 (sort [...])     (* iter 89 (op) section *)

Sum of [3;1;4;1;5;9;2;6;5;3;5] = 44 regardless of order, so the
result is also a smoke test of the implementation correctness — if
merge_sort drops or duplicates an element the sum diverges. 26
baseline programs total.
This commit is contained in:
2026-05-09 05:00:50 +00:00
parent 82ffc695a5
commit 7c40506571
3 changed files with 35 additions and 0 deletions

View File

@@ -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,

View File

@@ -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])