Skip to content

Instantly share code, notes, and snippets.

@austintaylor
Created April 20, 2011 15:02
Show Gist options
  • Save austintaylor/931566 to your computer and use it in GitHub Desktop.
Save austintaylor/931566 to your computer and use it in GitHub Desktop.
5 ways to do the same thing
products = concatMap (\x -> map (*x) [1..10]) [1..10]
products = [ x * y | x <- [1..10], y <- [1..10] ]
products = [1..10] >>= \x -> [1..10] >>= \y -> return $ x * y
products = do
x <- [1..10]
y <- [1..10]
return $ x * y
import Control.Applicative
products = (*) <$> [1..10] <*> [1..10]
@austintaylor
Copy link
Author

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