Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Recursive permutation generator via fold-pick-recurse:
let rec permutations xs = match xs with
| [] -> [[]]
| _ ->
List.fold_left (fun acc x ->
let rest = List.filter (fun y -> y <> x) xs in
let subs = permutations rest in
acc @ List.map (fun p -> x :: p) subs
) [] xs
For permutations of [1; 2; 3; 4] (24 total), count those whose
first element is less than the last:
match p with
| [a; _; _; b] when a < b -> count := !count + 1
| _ -> ()
By symmetry, exactly half satisfy a < b = 12.
Tests List.filter, recursive fold with append, fixed-length
list pattern [a; _; _; b] with multiple wildcards + when guard.
183 baseline programs total.
21 lines
421 B
OCaml
21 lines
421 B
OCaml
let rec permutations xs =
|
|
match xs with
|
|
| [] -> [[]]
|
|
| _ ->
|
|
List.fold_left (fun acc x ->
|
|
let rest = List.filter (fun y -> y <> x) xs in
|
|
let subs = permutations rest in
|
|
acc @ List.map (fun p -> x :: p) subs
|
|
) [] xs
|
|
|
|
;;
|
|
|
|
let ps = permutations [1; 2; 3; 4] in
|
|
let count = ref 0 in
|
|
List.iter (fun p ->
|
|
match p with
|
|
| [a; _; _; b] when a < b -> count := !count + 1
|
|
| _ -> ()
|
|
) ps;
|
|
!count
|