ocaml: phase 5.1 merge_two.ml baseline (merge two sorted lists, length*sum = 441)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Standard two-finger merge with nested match-in-match:
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'
Used as a building block in merge_sort.ml (iter 104) but called out
as its own baseline here.
merge [1;4;7;10] [2;3;5;8;9] = [1;2;3;4;5;7;8;9;10]
length 9, sum 49, product 441.
85 baseline programs total.
This commit is contained in:
@@ -48,6 +48,7 @@
|
||||
"memo_fib.ml": 75025,
|
||||
"mortgage.ml": 1073,
|
||||
"merge_sort.ml": 44,
|
||||
"merge_two.ml": 441,
|
||||
"module_use.ml": 3,
|
||||
"newton_sqrt.ml": 1414,
|
||||
"mutable_record.ml": 10,
|
||||
|
||||
14
lib/ocaml/baseline/merge_two.ml
Normal file
14
lib/ocaml/baseline/merge_two.ml
Normal file
@@ -0,0 +1,14 @@
|
||||
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 m = merge [1; 4; 7; 10] [2; 3; 5; 8; 9] in
|
||||
List.fold_left (+) 0 m * List.length m
|
||||
Reference in New Issue
Block a user