Haskell map/zip Vs. list comprehension

qrest picture qrest · Jul 4, 2010 · Viewed 7.6k times · Source

Which of the following are you most likely to write?

r = zip xs $ map sqrt xs

or

r = [(x, sqrt x) | x <- xs]

Sample code on the Internet seems to indicate that the former is more abundant and the preferred way.

Answer

Carl picture Carl · Jul 4, 2010

People who spend too much time in #haskell would probably write that as

r = map (id &&& sqrt) xs

(&&&) is a fun combinator defined in Control.Arrow. Its actual type signature is complicated because it's generalized to all instances of Arrow. But it's often used with the (->) instance of Arrow, which results in this type signature:

(&&&) :: (a -> b) -> (a -> c) -> a -> (b, c)