2 ms·
I'll quote from the documentation link I referenced: > For example, the following zips together two lists: [ (x, y) | x <- xs | y <- ys ] That's precisely
by tuukkah 4y ago
I'll quote from the documentation link I referenced:
> For example, the following zips together two lists:
[ (x, y) | x <- xs | y <- ys ]
That's precisely the difference between a normal list comprehension (one pipe) and a parallel list comprehension (multiple pipes).
For clarity, here's your normal list comprehension (with one pipe) that produces all the combinations instead:
[ (x, y) | x <- xs, y <- ys ]
And here's the full example from the article converted to Haskell and producing the exact same output:
{-# LANGUAGE ParallelListComp #-}
import Control.Monad (mapM)
elems = [ "water", "earth", "fire", "air" ]
nats = [ "tribes", "kingdom", "nation", "nomads" ]
main = mapM putStrLn
[ show idx ++ " - " ++ e ++ " " ++ n
| e <- elems
| n <- nats
| idx <- [0..]
]
EDIT: I suppose an explicit zip with an anonymous function looks more idiomatic though:
main = forM (zip3 elems nats [0..]) $ \(e, n, idx) ->
putStrLn (show idx ++ " - " ++ e ++ " " ++ n)
EDIT2: Best of both worlds with the list monad?
main = mapM putStrLn $ do
(e, n, idx) <- zip3 elems nats [0..]
[ show idx ++ " - " ++ e ++ " " ++ n ]