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.
29 lines
542 B
OCaml
29 lines
542 B
OCaml
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])
|