Set consolidation: Difference between revisions

Added F# version
(Added Racket)
(Added F# version)
Line 600:
<pre>[[K,I,F,G,H],[A,C,D,B]]
[[A,B,D]]</pre>
 
=={{header|F_Sharp|F#}}==
<lang fsharp>let (|SeqNode|SeqEmpty|) s =
if Seq.isEmpty s then SeqEmpty
else SeqNode ((Seq.head s), Seq.skip 1 s)
 
let SetDisjunct x y = Set.isEmpty (Set.intersect x y)
 
let rec consolidate s = seq {
match s with
| SeqEmpty -> ()
| SeqNode (this, rest) ->
let consolidatedRest = consolidate rest
for that in consolidatedRest do
if (SetDisjunct this that) then yield that
yield Seq.fold (fun x y -> if not (SetDisjunct x y) then Set.union x y else x) this consolidatedRest
}
 
[<EntryPoint>]
let main args =
let makeSeqOfSet listOfList = List.map (fun x -> Set.ofList x) listOfList |> Seq.ofList
List.iter (fun x -> printfn "%A" (consolidate (makeSeqOfSet x))) [
[["A";"B"]; ["C";"D"]];
[["A";"B"]; ["B";"C"]];
[["A";"B"]; ["C";"D"]; ["D";"B"]];
[["H";"I";"K"]; ["A";"B"]; ["C";"D"]; ["D";"B"]; ["F";"G";"H"]]
]
0</lang>
Output
<pre>seq [set ["C"; "D"]; set ["A"; "B"]]
seq [set ["A"; "B"; "C"]]
seq [set ["A"; "B"; "C"; "D"]]
seq [set ["A"; "B"; "C"; "D"]; set ["F"; "G"; "H"; "I"; "K"]]</pre>
 
=={{header|Go}}==
Anonymous user