3 ms·
One feature I rather like in Haskell is being able to zip with list comprehension syntax[1]: λ> [(x, y) | x <- [1..] | y <- [1..3]] [(1,1),(2,2),(3,3)]
by tikhonj 5y ago
One feature I rather like in Haskell is being able to zip with list comprehension syntax[1]:
λ> [(x, y) | x <- [1..] | y <- [1..3]]
[(1,1),(2,2),(3,3)]
This example is a bit silly—I would just write zip [1..] [1..3]—but it makes more complex expressions involving zips much easier to read at a glance. I looked through some real code I've written, and being able to do things like this is definitely nice:
toHashMap values = HashMap.fromList
[ convert name value
| name <- Schema.fldName <$> Schema.fields t
| value <- Vector.toList values
]
Of course, this particular example would be nicer if Haskell had something like dictionary comprehensions for building hash maps, but this isn't an awful substitute, and zipping multiple sequences together is useful in a lot of other configurations as well!
[1]: I should add that this requires enabling the somewhat misleadingly named `ParallelListComp` extension. Frankly, I think extensions like this should just be added to the language—this introduces new syntax, so it never changes previously valid code—but the Haskell community is very conservative about making changes to the core, standardized part of the language :/.
- ssivark 5y agoThat actually seems confusing. I would normally expect such code to perform a Cartesian product i.e. all pairs of (x,y).
- dwohnitmok 5y agoIt would be a Cartesian product if the second `|` was changed to `,` (as is the case in the article and most examples in this comment section).