Nya*_*iOS 3 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]
Run Code Online (Sandbox Code Playgroud)
但是这会生成所有子集,即[1,3]包括哪些我不想要?无论如何要修复此代码,或者我必须重新考虑我的方法.此外,我不想使用内置的库函数,想要正确的基础知识.
aug*_*tss 12
就像是
conseqPower = ([] :) . concatMap (tail . inits) . tails
Run Code Online (Sandbox Code Playgroud)