I'm trying to make a function that takes in a list, and if one of the elements is negative, then any elements in that list that are equal to its positive counterpart should be changed to 0. Eg, if there is a -2 in a list, then all 2's in that list should be changed to 0.
Any ideas why it only works for some cases and not others? I'm not understanding why this is, I've looked it over several times.
changeToZero [] = []
changeToZero [x] = [x]
changeToZero (x:zs:y:ws) | (x < 0) && ((-1)*(x) == y) = x : zs : 0 : changeToZero ws
changeToZero (x:xs) = x : changeToZero xs
changeToZero [-1,1,-2,2,-3,3]
-- [-1,1,-2,2,-3,3]
changeToZero [-2,1,2,3]
-- [-2,1,0,3]
changeToZero [-2,1,2,3,2]
-- [-2,1,0,3,2]
changeToZero [1,-2,2,2,1]
-- [1,-2,2,0,1]
x:zs:y:wsis equivalent to[x,zs,y] ++ ws. Thus,zswill always be a single element of the list; it will never be multiple elements (or no elements).a:b,ais always a single element andbis always a list; the expressiona:bis equivalent to[a] ++ b. As a consequence of the fact that:is right-associative, this means that in an expression (or pattern) such asa:b:c:d, the rightmost variable is "the rest of the list", and the other variables are individual elements.