hamburgerwhoselflearns
hamburgerwhoselflearns

Reputation: 109

What does this statement mean in Haskell?

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!

Upvotes: 5

Views: 147

Answers (2)

Robert Long
Robert Long

Reputation: 6812

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

Upvotes: 6

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476594

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

Upvotes: 4

Related Questions