Home > Blockchain >  "Pattern match is redundant" when using input list argument inside list comprehension
"Pattern match is redundant" when using input list argument inside list comprehension

Time:01-25

This is my code:

example = [1,-4,7,12]

positiveSum :: [Int] -> Int
positiveSum (x) = 0
positiveSum (x:xs) = result
  where 
    result = sum [y y | y <- xs, y > 0]

main = do
  print (positiveSum example)

When I run it I get:

Main.hs:5:1: warning: [-Woverlapping-patterns]
    Pattern match is redundant
    In an equation for `positiveSum': positiveSum (x : xs) = ...
  |
5 | positiveSum (x:xs) = result
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^...

I can't use 'xs' inside the list comprehension and I don't understand why. It's a reference, and I should be able to use it. Why is it redundant?

Edit:

The answer solved the problem, I was matching anything with the first (x). Also, I confused myself and was applying sum twice. This is the right code:

positiveSum :: [Int] -> Int
positiveSum [] = 0
positiveSum xs = result
  where 
    result = sum [x | x <- xs, x > 0]

CodePudding user response:

You seem to think positiveSum (x) = 0 will only match the case of an empty list, but that will in fact match anything, which makes the next line redundant since it will never get tried. You meant to write positiveSum [] = 0 there. Also, note that your second case, even though it will now run, will throw away the head of the list, which you probably didn't mean to do.

CodePudding user response:

Pattern matches go top to bottom, so every call to positiveSum returns 0. Since the pattern for the first rule is positiveSum (x) = 0, which matches everything, since every value can be bound to x. By making that rule the last one, you can avoid this issue:

positiveSum :: [Int] -> Int
positiveSum (x:xs) = result
  where 
    result = sum [y y | y <- xs, y > 0]
positiveSum (x) = 0

With this code, we only fall back to the 0 case if there isn't a tail for the argument, causing the expected behaviour.

  •  Tags:  
  • Related