Files
rose-ash/lib/ocaml/baseline/combinations.ml
giles fad81e0b0c
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
ocaml: phase 5.1 combinations.ml baseline (C(9, 4) = 126)
Pascal-recursion combination enumerator:

  let rec choose k xs =
    if k = 0 then [[]]
    else match xs with
      | [] -> []
      | h :: rest ->
        List.map (fun c -> h :: c) (choose (k - 1) rest)
        @ choose k rest

  C(9, 4) = |choose 4 [1; ...; 9]| = 126

Tests pure-functional enumeration with List.map + closure over h,
@ append, [] | h :: rest pattern match on shrinking input.

200 baseline programs total -- milestone.
2026-05-11 05:25:03 +00:00

13 lines
235 B
OCaml

let rec choose k xs =
if k = 0 then [[]]
else
match xs with
| [] -> []
| h :: rest ->
List.map (fun c -> h :: c) (choose (k - 1) rest)
@ choose k rest
;;
List.length (choose 4 [1; 2; 3; 4; 5; 6; 7; 8; 9])