Last active
December 2, 2020 15:52
-
-
Save prGmtc/26fcb562652586d5a6efc5bfcb8d1fc4 to your computer and use it in GitHub Desktop.
This file contains 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
#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