生成列表的子列表

Nya*_*iOS 3 haskell functional-programming

我是haskell的新手并且正在尝试编写一个函数来生成一个仅包含连续子集的powerset,例如:[1,2,3] - > [[],[1],[2],[3],[ 1,2],[2,3],[1,2,3]]

我在博客http://davidtran.doublegifts.c​​om/blog/?p=7上找到了这个

powerset :: [a] -> [[a]]
powerset []     = [[]]
powerset (x:xs) = powerset xs ++ map (x:) (powerset xs)
-- powerset (x:xs) = powerset xs ++ [x:xs' | xs' <- powerset xs]
Run Code Online (Sandbox Code Playgroud)

但是这会生成所有子集,即[1,3]包括哪些我不想要?无论如何要修复此代码,或者我必须重新考虑我的方法.此外,我不想使用内置的库函数,想要正确的基础知识.

aug*_*tss 12

就像是

conseqPower = ([] :) . concatMap (tail . inits) . tails
Run Code Online (Sandbox Code Playgroud)

  • 好吧,你可以写自己的.但是重用现有功能更方便. (2认同)