I was looking at Data.Set
, and I found out that it has no powerset
function. Why?
I can implement it like this:
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 doesn't seem the most efficient way of doing it. OK, I can also write
powerList :: [a] -> [[a]]
powerList = filterM (const [True, False])
but still, I wonder why Data.Set
has no powerset
function.
Also, what is the best way to write powerset :: (Ord a) => Set a -> Set (Set a)
?