[Haskell-cafe] List manipulation
sholderm at students.cs.uu.nl
Wed Jan 26 11:47:09 EST 2005
> I have two lists of Double with equal length and want to create a
> third one,
> in which each element is the sum of the corresponding element of the
> list and the second list.
> add2Img :: [Double] -> [Double] -> [Double]
> add2Img summand1 summand2 = sum
> where sum = [ (x+y) | x <- summand1, y <- summand2 ]
> What is wrong with the function above?
You're performing a point-wise addition, but instead add each element
of the second list to each element of the first list, yielding n * m
sums for list lengths n and m.
The function you're looking for is written
add2Img = zipWith (+)
More information about the Haskell-Cafe