Created
February 7, 2012 03:11
-
-
Save tcql/1756904 to your computer and use it in GitHub Desktop.
Haskell Powerset
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{- | |
Calling powerset with a list | |
will return a list of all combinations of elements in the list. Example: | |
powerset [1,2,3] | |
returns : [[1,2,3],[1,2],[1,3],[1],[2,3],[2],[3],[]] | |
-} | |
import Control.Monad | |
powerset :: [a] -> [[a]] | |
powerset xs = filterM (\x -> [True, False]) xs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment