Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary map in Haskell

Tags:

haskell

Is there a two-operand equivalent to map built into Haskell with a type signature of:

map2 :: (a -> b -> c) -> [a] -> [b] -> [c]

The following equivalence should hold:

map2 operator as bs === [operator a b | (a, b) <- zip as bs]

Example:

ghci> map2 (*) [2,3] [4,5]
[8,15]
like image 793
Grumdrig Avatar asked Jan 17 '26 21:01

Grumdrig


1 Answers

The function zipWith does what you need. There are also zipWith3 .. zipWith7 for cases with more than two lists.

like image 185
shang Avatar answered Jan 19 '26 18:01

shang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!