Reputation: 114
I have two lists for example:
[1,2,3,4,5]
and [1,2,3,4,5]
and I want to merge them to [2,4,6,8,10]
.
Is there an easy way to do this in haskell since there aint traditional loops?
Assume that both lists will be same size
Upvotes: 0
Views: 161
Reputation: 15586
Haskell doesn't have traditional loops, but it does have recursion:
foo :: [Integer] -> [Integer] -> [Integer]
foo [] [] = []
foo (x:xs) (y:ys) = x+y : foo xs ys
Or, as suggested by Willem Van Onsem, you can just use the function zipWith
:
foo = zipWith (+)
Note that if you don't specify the exact type of foo
, it will be infered as
foo :: (Num a) => [a] -> [a] -> [a]
Upvotes: 3