F# Lambda 签名
我正在阅读 Chris Smith 的《Programming F#》,当我遇到 Lambadas 时,我正在尝试弄清楚 F#。这是我得到的一个示例中的 lambda
let generatePowerOfFunc base = (fun exponent -> base ** exponent);;
,它接受某些内容并返回一个函数,但我没有得到的是该函数的签名,即 valgeneratePowerOfFunc : float ->浮动-> float
它怎么有三个浮点数而不是两个?当有这个方法时
let powerOfTwo =generatePowerOfFunc 2.0;;
它只有 2 个浮点数 val powerOfTwo : (float -> float)
也许我没有得到整个类型签名协议。任何帮助将不胜感激。谢谢
I'm reading Programming F# by Chris Smith right now trying to figure out F# when i come across Lambadas. Here is a lambda from one of the examples
let generatePowerOfFunc base = (fun exponent -> base ** exponent);;
I get that it takes in something and returns a function, but what i don't get is the Signature of this function which is val generatePowerOfFunc : float -> float -> float
How does it have three floats instead of two? And when there's this method
let powerOfTwo = generatePowerOfFunc 2.0;;
It only has 2 floats val powerOfTwo : (float -> float)
Maybe Im not getting the whole type signature deal. Any help would be much appreciated. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了 kongo2002 之外:
->
链中的最后一项是返回类型
,不是另一个参数。第一个接受两个浮点数并返回一个浮点数,第二个接受一个浮点数并返回一个浮点数。这样做的想法,而不是像
(float, float) : float
这样的东西,是你可以使用一个称为“柯里化”的概念。generatePowerOfFunc
的类型为float ->浮动-> float
,相当于float -> (float -> float)
,因此我们可以给它一个 float 并返回一个float -> float 类型的函数。 float
(我们可以给它另一个浮点数,并返回一个浮点数)。这意味着当您调用
generatePowerOfFunc 2. 4.
时,您将应用两次。一旦您应用2.
,一旦您应用4.
。In addition to kongo2002:
The last item in the
->
chain is thereturn type
, not another argument. The first accepts two floats and returns a float, and the second accepts one float and returns one.The idea of doing it like that, and not something like
(float, float) : float
, is that you can use a concept called "currying".generatePowerOfFunc
is of typefloat -> float -> float
, which is equivalent tofloat -> (float -> float)
, so we can give it a single float and get back a function of typefloat -> float
(and we can give it another float, and get back a float).This means that when you call
generatePowerOfFunc 2. 4.
, you apply twice. Once you apply2.
, and once you apply4.
.函数
generatePowerOfFunc
接受两个float
类型的参数并返回一个float
值:函数
powerOfTwo
就像一个偏函数仅接受一个float
参数(指数)并返回一个float
的函数应用程序。The function
generatePowerOfFunc
takes two arguments of typefloat
and returns afloat
value:The function
powerOfTwo
is like a partial function application that just takes onefloat
argument (the exponent) and returns afloat
.