Created
April 20, 2011 15:02
-
-
Save austintaylor/931566 to your computer and use it in GitHub Desktop.
5 ways to do the same thing
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
products = concatMap (\x -> map (*x) [1..10]) [1..10] |
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
products = [ x * y | x <- [1..10], y <- [1..10] ] |
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
products = [1..10] >>= \x -> [1..10] >>= \y -> return $ x * y |
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
products = do | |
x <- [1..10] | |
y <- [1..10] | |
return $ x * y |
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.Applicative | |
products = (*) <$> [1..10] <*> [1..10] |
List comprehensions always seemed kind of awkward to me, but I think they kind of win here for clarity, length, and not requiring an import.
Applicative rocks, obviously, and if your program is already using the applicative style, I would go with it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is how you would do it in ruby, for reference. It is conceptually equivalent to the first example.