Pattern match is redundant

10,664

Solution 1

When declaring functions, the argument variables are new names. Your quote and doubleQuote are shadowing the functions rather than invoking the functions for pattern matching. In this way the language of pattern matching subtly deviates from the language of right-hand-sided expressions. To achieve what you want, either do

isBorder :: Char -> Bool
isBorder '\'' = True
isBorder '\"' = True
isBorder _ = False

or

isBorder :: Char -> Bool
isBorder c | quote == c = True
isBorder c | doubleQuote == c = True
isBorder _ = False

The philosophy is to first bind the argument to name and then to use a guard to invoke an expression that evaluates to a boolean.

Solution 2

The pattern can only be matched against concrete values, not against identifiers.

So the compiler essentially sees:

isBorder x = True
isBorder x = True

since quote and doublequote aren't the defined identifers but the names of the parameters.

Share:
10,664
Andrey Bushman
Author by

Andrey Bushman

Programmer. My BitBuket account is here.

Updated on June 19, 2022

Comments

  • Andrey Bushman
    Andrey Bushman almost 2 years

    Haskell, Stack build tool.

    I have code:

    quote :: Char
    quote = '\''
    
    doubleQuote :: Char
    doubleQuote = '\"'
    
    isBorder :: Char -> Bool
    isBorder quote = True
    isBorder doubleQuote = True
    isBorder _ = False
    

    It will be compiled without erors, but I see the messages during the compilation:

    D:\haskell\real\app\Main.hs:34:1: warning: [-Woverlapping-patterns]
    Pattern match is redundant
    In an equation for `isBorder': isBorder doubleQuote = ...

    D:\haskell\real\app\Main.hs:35:1: warning: [-Woverlapping-patterns]
    Pattern match is redundant
    In an equation for `isBorder': isBorder _ = ...

    What does it mean? I don't see redundance...

  • ThreeFx
    ThreeFx over 7 years
    You can pack all the calls in one top-level function.
  • Andrey Bushman
    Andrey Bushman over 7 years
    > since quote and doublequote aren't the defined identifers but the names of the parameters. Hm... But I see that my code works right... So Haskell understands what I meaned in my definitions.
  • Andrey Bushman
    Andrey Bushman over 7 years
    Yes, I understand you and I think you are right. Hm... But I see that my code works right too... So Haskell understands what I meaned in my definitions. Look my full code example, please here: yadi.sk/d/MfyG7OmHwGmsV
  • ThreeFx
    ThreeFx over 7 years
    Really? Is isBorder '5' false?