Skip to content

Instantly share code, notes, and snippets.

@tcql
Created February 7, 2012 03:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcql/1756904 to your computer and use it in GitHub Desktop.
Save tcql/1756904 to your computer and use it in GitHub Desktop.
Haskell Powerset
{-
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