[Haskell-beginners] Weird (++) behavior when adding 2 vectors

Alexander Raasch info at alexraasch.de
Tue Oct 18 14:19:49 CEST 2011


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



More information about the Beginners mailing list