Created
April 25, 2011 19:16
-
-
Save austintaylor/941024 to your computer and use it in GitHub Desktop.
Powerset: Haskell vs. Ruby
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
import Control.Monad (filterM) | |
powerset :: [a] -> [[a]] | |
powerset = filterM (\x -> [True, False]) |
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
class Array | |
def powerset | |
return [[]] if empty? | |
self[1..-1].powerset.map {|a| [self[0]] + a} + self[1..-1].powerset | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment