[Haskell-cafe] List manipulation

Stefan Holdermans sholderm at students.cs.uu.nl
Wed Jan 26 11:47:09 EST 2005


Dmitri,

> 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 
> first
> list and the second list.

> <function>
> add2Img :: [Double] -> [Double] -> [Double]
> add2Img summand1 summand2 = sum
> 	where sum = [ (x+y) | x <- summand1, y <- summand2 ]
> </function>,

> 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 (+)

HTH,

Stefan



More information about the Haskell-Cafe mailing list