Skip to content

Instantly share code, notes, and snippets.

@prGmtc
Last active December 2, 2020 15:52
Show Gist options
  • Save prGmtc/26fcb562652586d5a6efc5bfcb8d1fc4 to your computer and use it in GitHub Desktop.
Save prGmtc/26fcb562652586d5a6efc5bfcb8d1fc4 to your computer and use it in GitHub Desktop.
#r @"nuget: Unquote"
open Swensen.Unquote
let rec combinationsRecursive list =
match list with
|x :: xs ->
let withX = xs |> List.map (fun y -> (x,y))
let others = combinationsRecursive xs
List.append withX others
| _ -> []
let combinationsComprehensions list =
let indexed = list |> List.indexed
[ for (i,x) in indexed do
for (j,y) in indexed.[i+1..] ->
(x,y)]
test <@ combinationsRecursive [1..4] = [(1, 2); (1, 3); (1, 4); (2, 3); (2, 4); (3, 4)] @>
test <@ combinationsRecursive [1..4] = combinationsComprehensions [1..4] @>
test <@ combinationsRecursive [1;2;1;4] = [(1, 2); (1, 1); (1, 4); (2, 1); (2, 4); (1, 4)] @>
test <@ combinationsRecursive [1;2;1;4] = combinationsComprehensions [1;2;1;4] @>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment