Haskell generate subsets

ぐ巨炮叔叔 提交于 2019-12-05 10:43:10

You have subsets already. So we need a function

filterSubs :: [[Int]] -> [[Int]]
filterSubs = --remove all subsets which don't sum to 0

So next we'd need a predicate

sumZero :: [Int] -> Bool
sumZero xs = sum xs == 0

Now, using this and filter it's easy to construct filterSubs. I'll leave this to you to figure out exactly how that works. And then our solution is trivial

zeroSubs = filterSubs . subsets

The idea is that you want to take all subsets and then only keep the ones that sum 0

subsetsSum0 ls = filter (\ss -> (sum ss) == 0) (subsets ls)

let's make this code point-free

subsetsSum0 = filter ((==0) . sum) . subsets
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!