Skip to content

Instantly share code, notes, and snippets.

@yvanin
Created January 1, 2015 21:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yvanin/9ffbd0a3bf8e183dee0a to your computer and use it in GitHub Desktop.
Save yvanin/9ffbd0a3bf8e183dee0a to your computer and use it in GitHub Desktop.
Producing k-combinations of a set of elements in F#
// assuming elements in a set do not repeat
let rec kCombinations k (set: 'a list) =
match k with
| 1 -> set |> List.map (fun x -> [x])
| _ ->
match set with
| [] -> []
| head::tail ->
(tail |> kCombinations (k - 1) |> List.map (fun x -> head::x))
@ (tail |> kCombinations k)
printfn "%A" ([1;2;3;4] |> kCombinations 2)
@ImaginaryDevelopment
Copy link

what about without repetition ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment