[英]How does Haskell evaluate this signature?
ggt_euklid :: Nat1 -> (Nat1 -> Nat1)
I am trying to learn partial application, I know that in this case, if the parentheses would be left out, I would get the same result, but I do not know how this signature should be evaluated. 我正在尝试学习部分应用程序,我知道在这种情况下,如果省略括号,我将得到相同的结果,但是我不知道应该如何评估此签名。
As far as I have understood, parentheses signify that it is a function? 据我了解,括号表示它是一个函数吗? Would that not imply that ggt_euklid takes a value Nat1 and returns a function?
这是否意味着ggt_euklid取值Nat1并返回函数?
Below is the complete function: 以下是完整的功能:
ggt_euklid x y
| x == y = x
|x>y =ggt_euklid(x-y) y
|x<y =ggt_euklid x (y-x)
You have understood the type signature correctly: it takes one argument and returns a function. 您已经正确理解了类型签名:它接受一个参数并返回一个函数。 That's how "multi-argument" functions in Haskell work: through currying.
Haskell中的“多参数”函数就是这样工作的:通过咖喱化。 You can see this in action by trying this equivalent implementation:
您可以通过尝试以下等效实现来查看实际情况:
ggt_euklid :: Nat1 -> (Nat1 -> Nat1)
ggt_euklid x = \y -> result
where result | x == y = x
| x > y = ggt_euklid (x-y) y
| x < y = ggt_euklid x (y-x)
Here I've introduced this fairly pointless result
variable as a thing to use your pattern guards on, but the idea is the same. 在这里,我已经介绍了这个相当毫无意义的
result
变量,可以将其用作模式防护,但是想法是相同的。
Would that not imply that ggt_euklid takes a value Nat1 and returns a function?
这是否意味着ggt_euklid取值Nat1并返回函数?
No , it still imply that ggt_euklid
takes one argument of type Nat1
and return a function of type Nat1->Nat1
, even though, the parentheses be left out . 不 ,它仍然意味着
ggt_euklid
需要一个类型的一个参数Nat1
和返回类型的函数Nat1->Nat1
,尽管中 , 括号去掉。
The Arrow ->
always be right-associativity (when no parentheses), ie: 箭头
->
始终是右关联性(没有括号时),即:
Nat1 -> Nat1 -> Nat1
is equivalent to 相当于
Nat1 -> (Nat1 -> Nat1)
which is corresponding to the function application always be left-associativity. 与功能应用程序相对应的总是左关联性。 (when no parentheses) for example:
(如果没有括号),例如:
ggt_euklid 1 2
is equivalent to 相当于
(ggt_euklid 1) 2
Here 这里
(ggt_euklid 1) ~ Nat1 -> Nat1
and 和
(ggt_euklid 1) 2 ~ Nat1
So, no matter whether one or two arguments apply to ggt_euklid
, it always return a function of type Nat1 -> Nat1
firstly, if second argument is provided, it applies second argument to the returned function. 所以,不管一个或两个参数是否适用于
ggt_euklid
,它总是返回类型的函数Nat1 -> Nat1
首先,如果提供了第二个参数,它适用于第二个参数返回的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.