Home > Software design >  How can I make my haskell code work for all of the tests?
How can I make my haskell code work for all of the tests?

Time:11-17

Put the vectors together in a list! Two vectors are added by adding together the same components.

data Vector = V Int Int Int 
vector :: [Vector] -> Vector
vector [] = (V 0 0 0)
vector [V x1 x2 x3] = (V x1 x2 x3)
vector [V x1 x2 x3, V y1 y2 y3] = (V (x1   y1) (x2   y2) (x3   y3))

CodePudding user response:

Haskell do not support iteration, so you need to know how to use basic recursion or just use foldl/foldr.

vectorListSum :: [Vector3] -> Vector3
vectorListSum = foldl (\(V x1 y1 z1) (V x2 y2 z2) -> V (x1   x2) (y1   y2) (z1   z2)) $ V 0 0 0

Every haskell tutorial will contain a same example, maybe you should read more carefully.

  • Related