我在看Data.Set
,发现它没有任何powerset
功能。为什么?
我可以像这样实现它:
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)
但这似乎不是最有效的方法。好的,我也可以写
powerList :: [a] -> [[a]]
powerList = filterM (const [True, False])
但是,我仍然想知道为什么Data.Set
没有powerset
功能。
另外,最好的写作方式是powerset :: (Ord a) => Set a -> Set (Set a)
什么?