为什么Data.Set没有powerset函数?
我查看了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) => 的最佳方式是什么?设置->设置(设置a)?
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)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有趣的是,前几天我实际上在 Haskell 中实现了 powerset 只是为了好玩 /r/python 上的评论。
这很像 camccann 在上面的评论中所描述的那样。
Set
的快速union
应该会比列表版本提高速度。Funny, I actually implemented
powerset
in Haskell the other day just for fun in a comment at /r/python.It is much as camccann described in his comment above.
Set
's fastunion
should give it a speed boost over the list version.