Skip to content

Instantly share code, notes, and snippets.

@telescreen
Last active December 10, 2015 05:38
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 telescreen/4388752 to your computer and use it in GitHub Desktop.
Save telescreen/4388752 to your computer and use it in GitHub Desktop.
-- Given a list (for instance [1,2,3]), return its powerset (a set of subsets)
-- Combinatorics guys: well a subset is a set with/without the first element
-- Hoola!
-- powerset [1,2,3] = [[],[3],[2],[2,3],[1],[1,3],[1,2],[1,2,3]]
powerset :: [a] -> [[a]]
powerset [] = [[]]
powerset (x:xs) = let ss = subset xs in ss ++ (map (\ys -> x:ys) ss)
-- Monad guys: after a few minutes thinking
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