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

Amy de Buitléir amy at nualeargais.ie
Tue Oct 18 14:51:27 CEST 2011


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




More information about the Beginners mailing list