
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]. I wrote this function <function> add2Img :: [Double] -> [Double] -> [Double] add2Img summand1 summand2 = sum where sum = [ (x+y) | x <- summand1, y <- summand2 ] </function>, but I'm getting [3.0,4.0,501.0,4.0,5.0,502.0,102.0,103.0,600.0] instead of [1, 2, 100]. What is wrong with the function above? TIA Dmitri Pissarenko -- Dmitri Pissarenko Software Engineer http://dapissarenko.com

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

Hello!
I have two lists of Double with equal length and want to create a
Dmitri Pissarenko wrote: 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

Thanks all for the help! -- Dmitri Pissarenko Software Engineer http://dapissarenko.com

On 26 Jan 2005, at 16:39, Dmitri Pissarenko wrote:
Hello!
Hi Dmitri. Have a browse around the haskell wiki! There's loads of interesting information and example code there...
add2Img summand1 summand2 = sum where sum = [ (x+y) | x <- summand1, y <- summand2 ]
[3.0,4.0,501.0,4.0,5.0,502.0,102.0,103.0,600.0]
instead of
[1, 2, 100].
[(x+y) | x <- summand1, y <- summand2] means *all* possible sums x+y with x taken from the first list and y from the second. This is the nature of list comprehensions. You rather want 'zipWith'. Documentation at: http://www.haskell.org/ghc/docs/latest/html/libraries/base/GHC.List.html ...along with lots of other funky list processing stuff. Jules

Jules Bean wrote:
[...] You rather want 'zipWith'. Documentation at:
http://www.haskell.org/ghc/docs/latest/html/libraries/base/GHC.List.html
...along with lots of other funky list processing stuff.
Just a small hint: Everything below "GHC" in the hierarchical libraries is, well, GHC-specific, meant for internal use only and may change without further notice, see "Stability: internal" in the page you mentioned. Just use http://haskell.org/ghc/docs/latest/html/libraries/base/Data.List.html instead. Cheers, S.

On 27 Jan 2005, at 07:32, Sven Panne wrote:
Jules Bean wrote:
[...] You rather want 'zipWith'. Documentation at: http://www.haskell.org/ghc/docs/latest/html/libraries/base/ GHC.List.html ...along with lots of other funky list processing stuff.
Just a small hint: Everything below "GHC" in the hierarchical libraries is, well, GHC-specific, meant for internal use only and may change without further notice, see "Stability: internal" in the page you mentioned. Just use http://haskell.org/ghc/docs/latest/html/libraries/base/Data.List.html instead.
Ah yes, apologies for misleading. That's what I get for bashing in a google search and copy-pasting the first URL I find instead of starting from the hierarchical libraries home page and looking for the right page :P Jules

Hi why don't you try something like this: map (\(x,y) -> x+y) (zip [1,2,100] [2,3,500]) list comprehension would sum every element of the firs list with every element of the second. On Wed, 2005-01-26 at 17:39 +0100, 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].
I wrote this function
<function> add2Img :: [Double] -> [Double] -> [Double] add2Img summand1 summand2 = sum where sum = [ (x+y) | x <- summand1, y <- summand2 ] </function>,
but I'm getting
[3.0,4.0,501.0,4.0,5.0,502.0,102.0,103.0,600.0]
instead of
[1, 2, 100].
What is wrong with the function above?
TIA
Dmitri Pissarenko -- Dmitri Pissarenko Software Engineer http://dapissarenko.com _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe --
Luca Marchetti Senior Application Specialist Bizmatica S.p.A. Via Pietrasanta, 14 - Building 3a 20141 Milano, Italy e-mail: luca.marchetti@bizmatica.com

On Wed, 26 Jan 2005, Luca Marchetti wrote:
Hi
why don't you try something like this:
map (\(x,y) -> x+y) (zip [1,2,100] [2,3,500])
list comprehension would sum every element of the firs list with every element of the second.
If 'zipWith (+)' doesn't satisfy you, what about map (uncurry (+)) (zip [1,2,100] [2,3,500]) ? B-]
participants (7)
-
Dmitri Pissarenko
-
Gracjan Polak
-
Henning Thielemann
-
Jules Bean
-
Luca Marchetti
-
Stefan Holdermans
-
Sven Panne