3 ms·
> The new multi-sequence syntax allows you to loop over two or more arrays or slices at the same time In Haskell, this is called a parallel list comprehension:
by tuukkah 4y ago
> The new multi-sequence syntax allows you to loop over two or more arrays or slices at the same time
In Haskell, this is called a parallel list comprehension:
[x+y | x <- xs | y <- ys]
In a normal list comprehension, you have a single pipe, in a parallel one you have as many pipes as how many lists you are zipping. https://downloads.haskell.org/ghc/latest/docs/users_guide/exts/parallel_list_comprehensions.html https://downloads.haskell.org/ghc/latest/docs/users_guide/ex...
- masklinn 4y agoNo, your list comprehension is a product (it iterates ys for every x). The feature here is zip.
- tuukkah 4y agoI'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 ]