简体   繁体   English

(==“”)在Haskell中是什么意思?

[英]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. 我尝试在GHCi中以不同的方式对其进行测试,但我无法找出任何可行的方法。 Can someone explain what it means, and this question will be a result the next time someone Googles Haskell (==" ") 有人可以解释它的意思吗,这个问题将在下次有人Googles Haskell(==“”)时产生

It's a section . 这是一 It's equivalent to recipe = \\x -> x == "000001" (which in turn is the same as recipe x = x == "000001" ). 它等效于recipe = \\x -> x == "000001" (又与recipe x = x == "000001" )。

You can use GHCI to figure this one out. 您可以使用GHCI来解决这一问题。

In GHCI , put in let recipe = (== "000001") . GHCI ,放入let recipe = (== "000001") Now we can see how it works. 现在我们可以看到它是如何工作的。 Try :t recipe to see what the type is. 尝试:t recipe ,看看类型是什么。 That returns recipe :: [Char] -> Bool , so it looks like this is a function that takes an list of Char s (a String ) and returns a Bool . 这将返回recipe :: [Char] -> Bool ,因此看起来这是一个接受Char列表( String )并返回Bool的函数。

If you test it, you'll find it returns False for any input except "000001" . 如果您对其进行测试,则会发现它对除"000001"以外的所有输入都返回False

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. 因此,这里== "000001"返回一个函数,该函数使用一个参数填充==的另一端并返回结果。


Edit: If the definition were recipe = ((==) "000001") this explanation would be right. 编辑:如果定义是recipe = ((==) "000001")此解释正确。

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 . ==函数的类型是a -> a -> Bool ,该函数接受两个相同类型的参数并返回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 . 但这也是a a -> (a -> Bool)类型a函数,它接受a类型a参数,并返回一个带有a -> Bool签名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. 我们为==提供了一个参数,因此在这种特殊情况下,它返回了a -> Bool[Char] -> Bool类型的新函数。

For binary operator @ the expression (@ x) would mean (\\y -> y @ x) . 对于二元运算符@ ,表达式(@ x)表示(\\y -> y @ x)

In your case it will be (\\y -> y == "000001") ie. 在您的情况下,它将是(\\y -> y == "000001")即。 function that takes String and says if it is equal to "000001" . 接受String并说是否等于"000001"函数。

(== arg) or (arg ==) is an operator section (it works for other operators as well - not just == ). (== arg)(arg ==)是一个运算符部分(它也适用于其他运算符-不仅仅是== )。 What it does is to partially apply the operator to the given operand. 它的作用是将运算符部分地应用于给定的操作数。 So (== "foo") is the same as \\x -> x == "foo" . 因此(== "foo")\\x -> x == "foo"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM