I want to calculate the strength of the set. Because I do not need all the power at one time, it is better to create it lazily.
For instance:
powerset (set ["a"; "b"; "c"]) = seq { set []; set ["a"]; set ["b"]; set ["c"]; set ["a"; "b"]; set ["a"; "c"]; set ["b"; "c"]; set ["a";"b"; "c"]; }
Since the result is a sequence, I prefer it in that order. How can I do this ideally in F #?
EDIT:
This is what I am going to use (based on BLUEPIXY answer):
let powerset s = let rec loop nl = seq { match n, l with | 0, _ -> yield [] | _, [] -> () | n, x::xs -> yield! Seq.map (fun l -> x::l) (loop (n-1) xs) yield! loop n xs } let xs = s |> Set.toList seq { for i = 0 to List.length xs do for x in loop i xs -> set x }
Thank you all for the excellent input.
source share