Weird (++) behavior when adding 2 vectors

Hi, so I wrote this function to add two vectors represented as lists: add a b = add' a b [] where add' [] [] s = s add' (a:as) (b:bs) s ++ [a+b] OK, I tried this in ghci:
add [1,2,3] [1,2,3] [6,4,2]
Hm, I expected the result to be [2,4,6], of course, since the currently added components always go to the end of the resulting vector. I then changed the last term in add' to [a+b] ++ s , but still
add [1,2,3] [1,2,3] [6,4,2]
Can anyone explain this behavior to me. I think there should be some change in the result, no? Alex

Hi, Your code is not valid syntactically, I guess the last equation for add' is "add' (a:as) (b:bs) s = add' as bs s ++ [a+b]". In that case, the function application binds stronger that ++ operator so the expression is actually equivalent to "(add' as bs s) ++ [a+b]". So you can easily see that the list is computed backwards. Vlad On 18 Oct 2011, at 14:19, Alexander Raasch wrote:
Hi,
so I wrote this function to add two vectors represented as lists:
add a b = add' a b [] where add' [] [] s = s add' (a:as) (b:bs) s ++ [a+b]
OK, I tried this in ghci:
add [1,2,3] [1,2,3] [6,4,2]
Hm, I expected the result to be [2,4,6], of course, since the currently added components always go to the end of the resulting vector. I then changed the last term in add' to
[a+b] ++ s ,
but still
add [1,2,3] [1,2,3] [6,4,2]
Can anyone explain this behavior to me. I think there should be some change in the result, no?
Alex
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Hi,
On Tue, Oct 18, 2011 at 1:19 PM, Alexander Raasch
Hi,
so I wrote this function to add two vectors represented as lists:
add a b = add' a b [] where add' [] [] s = s add' (a:as) (b:bs) s ++ [a+b]
I think something mangled your function, as this is not valid Haskell code. Anyway, I tried to rewrite your function. The first version works as expected; the second gives reversed output. Note that there is no need for the accumulator "s". add a b = add' a b where add' [] [] = [] add' (a:as) (b:bs) = [a+b] ++ (add' as bs) add a b = add' a b where add' [] [] = [] add' (a:as) (b:bs) = (add' as bs) ++ [a+b] -- reversed output Obviously, the same function can be written as: zipWith (+) [1,2,3] [1,2,3] hth, L.

Hi, thanks for your answers. I'm exercising with different programming techniques, so the use of an accumulator was intentional although not necessary. As Vlad said, my mistake was the stronger binding of the function application. Sigh, ... Thank you all again. Alex On 10/18/2011 02:48 PM, Lorenzo Bolla wrote:
Hi,
On Tue, Oct 18, 2011 at 1:19 PM, Alexander Raasch
mailto:info@alexraasch.de> wrote: Hi,
so I wrote this function to add two vectors represented as lists:
add a b = add' a b [] where add' [] [] s = s add' (a:as) (b:bs) s ++ [a+b]
I think something mangled your function, as this is not valid Haskell code.
Anyway, I tried to rewrite your function. The first version works as expected; the second gives reversed output. Note that there is no need for the accumulator "s".
add a b = add' a b where add' [] [] = [] add' (a:as) (b:bs) = [a+b] ++ (add' as bs)
add a b = add' a b where add' [] [] = [] add' (a:as) (b:bs) = (add' as bs) ++ [a+b] -- reversed output
Obviously, the same function can be written as: zipWith (+) [1,2,3] [1,2,3]
hth, L.

There seems to be something missing from this line:
add' (a:as) (b:bs) s ++ [a+b]
Assuming you want to write your own function as an exercise, you could write it as a recursive function like this: add [] b = b add a [] = a add (a:as) (b:bs) = (a+b) : (add as bs) FYI, the easiest way to accomplish what you want is: add = zipWith (+) which is equivalent to: add a b = zipWith (+) a b Hope that helps
participants (4)
-
Alexander Raasch
-
Amy de Buitléir
-
Lorenzo Bolla
-
Vlad Hanciuta