Why doesn't Data.Set have a poweret function?

I looked at Data.Set and I found out that it does not have powerset function. Why?

I can implement it as follows:

 import Data.Set (Set, empty, fromList, toList, insert) powerset :: (Ord a) => Set a -> Set (Set a) powerset s = fromList $ map (fromList) (powerList $ toList s) powerList :: [a] -> [[a]] powerList [] = [[]] powerList (x:xs) = powerList xs ++ map (x:) (powerList xs) 

But this is not the most effective way to do this. Ok i can also write

 powerList :: [a] -> [[a]] powerList = filterM (const [True, False]) 

but still, I wonder why Data.Set does not have a powerset function.

Also, what is the best way to write powerset :: (Ord a) => Set a -> Set (Set a) ?

+7
source share
1 answer

Funny, I actually implemented powerset in Haskell the other day just for the fun of commenting on / r / python .

 import Data.Set import Prelude hiding (map) powerset s | s == empty = singleton empty | otherwise = map (insert x) pxs `union` pxs where (x, xs) = deleteFindMin s pxs = powerset xs 

This is as described in his comment above. Set fast union should give it speed acceleration over the list version.

+12
source

All Articles