[Haskell-cafe] List manipulation

Gracjan Polak gracjan at acchsh.com
Wed Jan 26 11:49:47 EST 2005



Dmitri Pissarenko wrote:
 > Hello!
 >
 > 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.
 >
 > If list1 is [1, 2, 100] and list2 is [2, 3, 500], then the result of the
 > operation I desire is [3, 5, 600].

zipWith (+) [1,2,100] [2,3,500]

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

This is intepreted as two nestes "loops": foreach x in summand1 (foreach 
y in summand2: x + y). You need zipWith.

There is GHC extension: parallel list composition to do what you want. 
Lookup GHC documentation for extensions.

-- 
Gracjan


More information about the Haskell-Cafe mailing list