Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powerset Without Duplicates

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.

like image 866
lepdeffard Avatar asked Mar 27 '15 11:03

lepdeffard


1 Answers

  1. Remove all duplicates from the given list(you can use the nub function).

  2. Run the algorithm you are using now.

like image 101
kraskevich Avatar answered Oct 21 '22 05:10

kraskevich