Haskell generate subsets

后端 未结 2 1214
南方客
南方客 2021-01-13 00:22

I have a function \'subsets\' which generate all the subsets of a given set:

subsets :: [Int] -> [[Int]]
subsets []  = [[]]
subsets (x:xs) = subsets xs ++         


        
2条回答
  •  抹茶落季
    2021-01-13 00:52

    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
    

提交回复
热议问题