CodyBugstein
CodyBugstein

Reputation: 23322

What does (== " ") mean, in Haskell?

To the vultures who might say "Look it up in your textbook", or "Hoogle it", I did.

I came across the statement

recipe = (== "000001")

It looks like some sort of boolean to me but I'm not sure. I've tried testing it in different ways in GHCi but I couldn't figure out anything that works. Can someone explain what it means, and this question will be a result the next time someone Googles Haskell (==" ")

Upvotes: 7

Views: 1274

Answers (4)

Jeff Burka
Jeff Burka

Reputation: 2571

You can use GHCI to figure this one out.

In GHCI, put in let recipe = (== "000001"). Now we can see how it works. Try :t recipe to see what the type is. That returns recipe :: [Char] -> Bool, so it looks like this is a function that takes an list of Chars (a String) and returns a Bool.

If you test it, you'll find it returns False for any input except "000001".

Since == is an operator, you can partially apply it to one argument, and it will return a function that takes the other argument and returns the result. So here == "000001" returns a function that takes one argument to fill in the other side of the == and returns the result.


Edit: If the definition were recipe = ((==) "000001") this explanation would be right.

To understand this, you should look up partial application. The type of the == function is a -> a -> Bool, a function that takes two arguments of the same type and returns a Bool.

But it's also a function of type a -> (a -> Bool), that takes one argument of type a and returns a new function with the signature a -> Bool. That's what's happening here. We've supplied one argument to ==, so it returned a new function of type a -> Bool, or [Char] -> Bool in this particular case.

Upvotes: 12

Cat Plus Plus
Cat Plus Plus

Reputation: 129764

It's a section. It's equivalent to recipe = \x -> x == "000001" (which in turn is the same as recipe x = x == "000001").

Upvotes: 27

sepp2k
sepp2k

Reputation: 370162

(== arg) or (arg ==) is an operator section (it works for other operators as well - not just ==). What it does is to partially apply the operator to the given operand. So (== "foo") is the same as \x -> x == "foo".

Upvotes: 5

zch
zch

Reputation: 15278

For binary operator @ the expression (@ x) would mean (\y -> y @ x).

In your case it will be (\y -> y == "000001") ie. function that takes String and says if it is equal to "000001".

Upvotes: 7

Related Questions