Created
January 1, 2015 21:19
-
-
Save yvanin/9ffbd0a3bf8e183dee0a to your computer and use it in GitHub Desktop.
Producing k-combinations of a set of elements in F#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what about without repetition ?