“int - > int - > INT"这在F#中意味着什么? [英] "int -> int -> int" What does this mean in F#?
问题描述
我想知道这在F#中意味着什么。
是一个取整数的函数,
返回一个带整数并返回整数的函数。
但我不明白这一点。
任何人都可以解释清楚吗?
[更新]:
>让f1 x y = x + y ;;
val f1:int - > int - > int
这是什么意思?
F#类型
让我们从头开始。 冒号(:
)符号来表示事物的类型。假设您定义了一个类型为 int
的值:
let myNumber = 5
F#Interactive会明白 myNumber
是一个整数,并会告诉你:
myNumber:int
读作
myNumber
类型为int
F#函数类型
到目前为止这么好。我们来介绍一些其他的功能类型。功能类型只是类型的功能。 F#使用 - >
来表示功能类型。这个箭头象征着左手边写的东西被转化为右手边的东西。
让我们考虑一个简单的函数,它需要一个参数并将其转换为一个输出。这样一个函数的例子是:
isEven:int - > bool
这将介绍函数的名称(位于左侧):
)及其类型。这行可以用英文阅读:
isEven
是一个函数类型将int
转换为bool
。
请注意,要正确解释所说的内容,您应该在类型部分后短暂停顿,然后立即阅读其余部分,而不是暂停。
在F#函数中是值
在F#中,函数几乎没有比普通的 >类型。它们是你可以传递给函数的东西,从函数返回,就像bools,ints或者字符串一样。 所以如果你有: 您应该考虑 包含
myNumber:int
isEven:int - > bool
int
和 int - > bool
作为两个相同类型的实体:类型。在这里, myNumber
是类型 int
的值, isEven
是类型 int - >的值。 bool
(这是我在上面讨论 short pause 时想象征的内容)。
函数应用程序
- >
的类型的值恰好也称为函数,并且具有特殊的功能:您可以将
isEven myNumber
表示您正在将 isEven
函数应用于值 myNumber
。正如您所期望的那样,通过检查 isEven
的类型,它将返回一个布尔值。如果你已经正确地执行 isEven
,它显然会返回 false
。
返回函数类型值的函数
让我们定义一个通用函数来确定一个整数是其他整数的整数倍。我们可以想象,我们的函数的类型是(括号在这里帮助你理解,它们可能或可能不存在,它们有特殊的含义):
isMultipleOf:int - > (int - > bool)
正如你所猜测的那样:
$ b
isMultipleOf
是类型(PAUSE)函数,它转换int
转换为(PAUSE)函数,将int
转换为bool
。
(这里(PAUSE)表示大声朗读时的暂停)。
我们会稍后定义此功能。在此之前,我们来看看我们如何使用它:
let isEven = isMultipleOf 2
F#interactive会回答:
isEven: int - > bool
读取为
isEven
类型为int - > bool
这里, isEven
code> int - > bool ,因为我们给了 isMultipleOf
的值2( int
), ,正如我们已经看到的那样,将 int
转换为 int - > bool
。
我们可以将 isMultipleOf
作为一种函数创建器。
的定义isMultipleOf
let isMultipleOf nx =
(x%n )= 0
很简单,呵呵?
如果您在F#Interactive中输入此值,它将回答:
isMultipleOf:int - > int - > bool
圆括号在哪里?
请注意,没有括号。这对你现在并不特别重要。请记住,箭头是正确的关联。也就是说,如果您有
a - > b - > c
您应该将其解释为
a - > (b - > c)
右侧右侧右侧联想意味着您应该解释为最右边的运算符周围有括号。所以:
a - > b - > c - > d
应解释为
a - > (b - >(c - > d))
isMultipleOf
因此,如您所见,我们可以使用 isMultipleOf
来创建新的函数:
let isEven = isMultipleOf 2
let isOdd = not<< isEven
let isMultipleOfThree = isMultipleOf 3
let endsWithZero = isMultipleOf 10
F#Interactive会回应:
isEven:int - > bool
isOdd:int - > bool
isMultipleOfThree:int - > bool
endsWithZero:int - > bool
但您可以以不同的方式使用它。如果你不想(或者需要)创建一个新的函数,你可以按如下方式使用它:
isMultipleOf 10 150
这会返回 true
,如150是10的倍数。这与创建函数 endsWithZero
然后将其应用于值150完全相同。
<实际上,功能应用程序是关联的,这意味着上面的行应该被解释为:
(isMultipleOf 10)150
即,将括号放在最左边的函数应用程序中。 / p>
现在,如果你能理解所有这些,你的例子(它是规范 CreateAdder
)应该是微不足道的! / p>
前段时间有人问过 我希望这有助于。 b $ b I wonder what this means in F#. [Update]: What this mean ? Let's begin from the beginning. F# uses the colon ( F# Interactive will understand that which is read as
So far so good. Let's introduce something else, functional types. A functional type is simply the type of a function. F# uses Let's consider a simple function, that takes one argument and transforms it into one output. An example of such a function would be: This introduces the name of the function (on the left of the Note that to correctly interpret what is being said, you should make a short pause just after the part "is of type", and then read the rest of the sentence at once, without pausing. In F#, functions are (almost) no more special than ordinary types. They are things that you can pass around to functions, return from functions, just like bools, ints or strings. So if you have: You should consider Values of types that contain means that you are applying the function called Let's define a generic function to determine is an integer is multiple of some other integer. We can imagine that our function's type will be (the parenthesis are here to help you understand, they might or might not be present, they have a special meaning): As you can guess, this is read as: (here the (PAUSE) denote the pauses when reading out loud). We will define this function later. Before that, let's see how we can use it: F# interactive would answer: which is read as Here, We can view this function So now let's define this mystical function-creating function. Easy, huh? If you type this into F# Interactive, it will answer:
Note that there are no parenthesis. This is not particularly important for you now. Just remember that the arrows are right associative. That is, if you have you should interpret it as The right in right associative means that you should interpret as if there were parenthesis around the rightmost operator. So: should be interpreted as
So, as you have seen, we can use F# Interactive would respond: But you can use it differently. If you don't want to (or need to) create a new function, you can use it as follows: This would return Actually, function application is left associative, which means that the line above should be interpreted as: That is, you put the parenthesis around the leftmost function application. Now, if you can understand all this, your example (which is the canonical Sometime ago someone asked this question which deals with exactly the same concept, but in Javascript. In my answer I give two canonical examples (CreateAdder, CreateMultiplier) inf Javascript, that are somewhat more explicit about returning functions. I hope this helps. 这篇关于“int - > int - > INT"这在F#中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
"a function taking an integer,
which returns a function which takes an integer and returns an integer."
But I don't understand this well.
Can anyone explain this so clear ? > let f1 x y = x+y ;;
val f1 : int -> int -> int
F# types
:
) notation to indicate types of things. Let's say you define a value of type int
:let myNumber = 5
myNumber
is an integer, and will tell you this by:myNumber : int
myNumber
is of type int
F# functional types
->
to denote a functional type. This arrow symbolizes that what is written on its left-hand side is transformed into what is written into its right-hand side.isEven : int -> bool
:
), and its type. This line can be read in English as:
isEven
is of type function that transforms an int
into a bool
.In F# functions are values
myNumber : int
isEven : int -> bool
int
and int -> bool
as two entities of the same kind: types. Here, myNumber
is a value of type int
, and isEven
is a value of type int -> bool
(this is what I'm trying to symbolize when I talk about the short pause above).Function application
->
happens to be also called functions, and have special powers: you can apply a function to a value. So, for example,isEven myNumber
isEven
to the value myNumber
. As you can expect by inspecting the type of isEven
, it will return a boolean value. If you have correctly implemented isEven
, it would obviously return false
.A function that returns a value of a functional type
isMultipleOf : int -> (int -> bool)
isMultipleOf
is of type (PAUSE) function that transforms an int
into (PAUSE) function that transforms an int
into a bool
.let isEven = isMultipleOf 2
isEven : int -> bool
isEven
is of type int -> bool
isEven
has type int -> bool
, since we have just given the value 2 (int
) to isMultipleOf
, which, as we have already seen, transforms an int
into an int -> bool
.isMultipleOf
as a sort of function creator.Definition of
isMultipleOf
let isMultipleOf n x =
(x % n) = 0
isMultipleOf : int -> int -> bool
Where are the parenthesis?
a -> b -> c
a -> (b -> c)
a -> b -> c -> d
a -> (b -> (c -> d))
Usages of
isMultipleOf
isMultipleOf
to create new functions:let isEven = isMultipleOf 2
let isOdd = not << isEven
let isMultipleOfThree = isMultipleOf 3
let endsWithZero = isMultipleOf 10
isEven : int -> bool
isOdd : int -> bool
isMultipleOfThree : int -> bool
endsWithZero : int -> bool
isMultipleOf 10 150
true
, as 150 is multiple of 10. This is exactly the same as create the function endsWithZero
and then applying it to the value 150.(isMultipleOf 10) 150
CreateAdder
) should be trivial!