Created
May 22, 2017 12:31
-
-
Save fpawel/d39209ddbec53a49420079f2a726c102 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
let triples n = | |
let mutable triples = [] | |
let mutable ones = [] | |
let mutable pairs = | |
[ for x = 1 to n+1 do | |
for y = x+1 to n+1 do | |
yield x,y] | |
let del k = pairs <- List.filter( (<>) k) pairs | |
while not pairs.IsEmpty do | |
let (x,y) as xy = pairs.Head | |
del xy | |
match List.tryFind(fun (a,_) -> a = y) pairs with | |
| None -> | |
ones <- xy :: ones | |
| Some ((a,b) as ab) -> | |
del ab | |
match List.tryFind((=) (x,b) ) pairs with | |
| None -> | |
triples <- [xy; ab] :: triples | |
| Some xb -> | |
del xb | |
triples <- [ xy; ab; xb ] :: triples | |
pairs <- ones | |
while not pairs.IsEmpty do | |
let (x,y) as xy = pairs.Head | |
del xy | |
match List.tryFind(fun (a,b) -> a = x || a = y || b=x || b = y) pairs with | |
| None -> | |
triples <- [xy] :: triples | |
| Some ((a,b) as ab) -> | |
del ab | |
triples <- [xy; ab] :: triples | |
triples |
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
--- | |
> List.iteri (printfn "%d : %A") (triples 7) ;; | |
0 : [(1, 8)] | |
1 : [(5, 8); (4, 8)] | |
2 : [(3, 6); (6, 8); (3, 8)] | |
3 : [(3, 5); (5, 7)] | |
4 : [(3, 4); (4, 7); (3, 7)] | |
5 : [(2, 7); (7, 8); (2, 8)] | |
6 : [(2, 5); (5, 6)] | |
7 : [(2, 4); (4, 6); (2, 6)] | |
8 : [(1, 6); (6, 7); (1, 7)] | |
9 : [(1, 4); (4, 5); (1, 5)] | |
10 : [(1, 2); (2, 3); (1, 3)] | |
val it : unit = () | |
> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment