Home > Enterprise >  What does this statement mean in Haskell?
What does this statement mean in Haskell?

Time:04-18

I'm currently still getting to know more about Haskell, under the topic of Pattern Matching, what does it mean by _ && _ = False in Haskell? I came across this statement in one of the lecture slides, like what does it mean by the underscores? Thanks again!

CodePudding user response:

The underscores are wildcards. That is, they match any value, but do not bind the value to a name. A common example of this usage is:

True && True = True
_ && _ = False

CodePudding user response:

Underscores means a wildcard. It binds with any value. It thus means that for any value for the left and right operand for the given type of the function, it will fire that clause, and thus return False.

One can thus define (&&) :: Bool -> Bool -> Bool with:

(&&) :: Bool -> Bool -> Bool
True && True = True
_ && _ = False

The real implementation of (&&) is however lazy in its second parameter, and is implemented as [src]:

-- | Boolean \"and\"
(&&)                    :: Bool -> Bool -> Bool
True  && x              =  x
False && _              =  False
  • Related