生成列表的子列表

时间:2012-03-22 23:14:53

标签: haskell functional-programming

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

我在博客上发现了这个http://davidtran.doublegifts.com/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]

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

2 个答案:

答案 0 :(得分:12)

这样的东西
conseqPower = ([] :) . concatMap (tail . inits) . tails

答案 1 :(得分:0)

过滤掉非“连续”列表。

相关问题