Last active
December 10, 2015 05:38
-
-
Save telescreen/4388752 to your computer and use it in GitHub Desktop.
This file contains 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
-- 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