Search code examples
haskellpattern-matchingguard

Haskell - wildcard use on right side of guard in patterns


Lets say i have a piece of code like this:

test pattern
 | pattern == (_,NOT (WIRE _)) = 1     
 | pattern == (_,AND (WIRE _) (WIRE _)) = 2
 | otherwise = 0

Where i am trying to match it against one of several possibilities, some with one (WIRE ""), some with two. I have actual input as follows e.g.: ("p",NOT (WIRE "x")). I would like to have a pattern that could accept any letter as input (what i was hoping for with the _) and am failing dismally (illegal _). Is it possible to do this in haskell?


Solution

  • OK, this makes a lot more sense after the edit.

    == compares values, but _ is a pattern. Patterns appear in only the following syntactic contexts:

    • on the left hand side of a (pattern) binding, i.e. "what's before =", at top level or in where blocks or in let expressions or commands (in do notation);
    • to the left of <- in do notation or in a list comprehension;
    • to the left of -> in a case expression;
    • as the formal arguments of functions, either in function bindings or lambda (\) expressions.

    (I hope I haven't forgotten any!) In your case, you can achieve what you want by simply writing

    test (_, NOT (WIRE _)) = 1
    test (_, AND (WIRE _) (WIRE _)) = 2
    test _ = 0
    

    You might ask what is the correct version of "pattern == (_, NOT (WIRE _))". Well, you can write:

    case pattern of
      (_, NOT (WIRE _)) -> True
      _ -> False