简体   繁体   English

F#Lambda签名

[英]F# Lambda Signature

I'm reading Programming F# by Chris Smith right now trying to figure out F# when i come across Lambadas. 我现在正在阅读Chris Smith的《 Programming F#》,试图在遇到Lambadas时弄清楚F#。 Here is a lambda from one of the examples 这是其中一个示例的lambda

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 我知道它需要一些东西并返回一个函数,但是我没有得到的是该函数的签名,它是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) 它只有2个浮点数val powerOfTwo : (float -> float)

Maybe Im not getting the whole type signature deal. 也许我没有得到完整的类型签名协议。 Any help would be much appreciated. 任何帮助将非常感激。 Thanks 谢谢

In addition to kongo2002: 除了kongo2002:

The last item in the -> chain is the return type , not another argument. ->链中的最后一项是return type而不是另一个参数。 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". 这样做而不是(float, float) : float的想法是,您可以使用一个称为“ currying”的概念。 generatePowerOfFunc is of type float -> float -> float , which is equivalent to float -> (float -> float) , so we can give it a single float and get back a function of type float -> float (and we can give it another float, and get back a float). generatePowerOfFunc的类型为float -> float -> float ,等效于float -> (float -> float) ,因此我们可以给它一个float并返回一个float -> float类型的函数(我们可以给出另一个浮动,然后返回一个浮动)。

This means that when you call generatePowerOfFunc 2. 4. , you apply twice . 这意味着,当您调用generatePowerOfFunc 2. 4. ,将应用两次 Once you apply 2. , and once you apply 4. . 一旦申请了2. ,并且一旦申请了4. .。

The function generatePowerOfFunc takes two arguments of type float and returns a float value: 函数generatePowerOfFunc接受两个float类型的参数,并返回一个float值:

val generatePowerOfFunc : float -> float -> float
//                        ^^^^^^^^^^^^^^
//                        arguments
//                                          ^^^^^
//                                          return value

The function powerOfTwo is like a partial function application that just takes one float argument (the exponent) and returns a float . 函数powerOfTwo就像一个部分函数应用程序,仅使用一个float参数(指数)并返回float

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

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