I need to create a powerset function in haskell which takes a set and outputs the power set without duplicate entries, regardless of what is put in the input list. For example: [1,1] should return [[],[1]]
powerset [] = [[]]
powerset (x:xs) = union((powerset xs)) (map (x:) (powerset xs))
Where union is a previously defined function adjoins two sets without duplicates. The problem with the above code is that it counts duplicates as original entries so input [1,1] returns [[],[1],[1],[1,1]].
Any ideas? I've thought of using union with the input list and the empty list to scrub out duplicates, prior to triggering powerset, but I'm not sure how that would look.
Remove all duplicates from the given list(you can use the nub
function).
Run the algorithm you are using now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With