Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
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.
14 lines
334 B
OCaml
14 lines
334 B
OCaml
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
|