Powerset without duplicates

I need to create a poweret function in haskell that takes a set and displays a set of power without duplicate entries, regardless of what is placed in the input list. For example: [1,1] should return [[], [1]]

powerset [] = [[]] powerset (x:xs) = union((powerset xs)) (map (x:) (powerset xs)) 

If the union is a previously defined function, then adjoins two sets without duplicates. The problem with the above code is that it considers the duplicates as the source, so entering [1,1] returns [[], [1], [1], [1,1]].

Any ideas? I was thinking of combining with an input list and an empty list to clean up duplicates before starting the power train, but I'm not sure how it will look.

+5
source share
1 answer
  • Remove all duplicates from this list (you can use the nub function).

  • Run the algorithm you are using now.

+5
source

Source: https://habr.com/ru/post/1216273/


All Articles