“int - > int - > INT"这在F#中意味着什么? [英] "int -> int -> int" What does this mean in F#?

查看:168
本文介绍了“int - > int - > INT"这在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#.
"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 ?

[Update]:

> let f1 x y = x+y ;;

 val f1 : int -> int -> int

What this mean ?

解决方案

F# types

Let's begin from the beginning.

F# uses the colon (:) notation to indicate types of things. Let's say you define a value of type int:

let myNumber = 5

F# Interactive will understand that myNumber is an integer, and will tell you this by:

myNumber : int

which is read as

myNumber is of type int

F# functional types

So far so good. Let's introduce something else, functional types. A functional type is simply the type of a function. F# uses -> 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.

Let's consider a simple function, that takes one argument and transforms it into one output. An example of such a function would be:

isEven : int -> bool

This introduces the name of the function (on the left of the :), and its type. This line can be read in English as:

isEven is of type function that transforms an int into a bool.

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 values

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:

myNumber : int
isEven : int -> bool

You should consider 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

Values of types that contain -> happens to be also called functions, and have special powers: you can apply a function to a value. So, for example,

isEven myNumber

means that you are applying the function called 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

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):

isMultipleOf : int -> (int -> bool)

As you can guess, this is read as:

isMultipleOf is of type (PAUSE) function that transforms an int into (PAUSE) function that transforms an int into a bool.

(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:

let isEven = isMultipleOf 2

F# interactive would answer:

isEven : int -> bool

which is read as

isEven is of type int -> bool

Here, 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.

We can view this function isMultipleOf as a sort of function creator.

Definition of isMultipleOf

So now let's define this mystical function-creating function.

let isMultipleOf n x =
    (x % n) = 0

Easy, huh?

If you type this into F# Interactive, it will answer:

isMultipleOf : int -> int -> bool

Where are the parenthesis?

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

a -> b -> c

you should interpret it as

a -> (b -> c)

The right in right associative means that you should interpret as if there were parenthesis around the rightmost operator. So:

a -> b -> c -> d

should be interpreted as

a -> (b -> (c -> d))

Usages of isMultipleOf

So, as you have seen, we can use isMultipleOf to create new functions:

let isEven = isMultipleOf 2
let isOdd = not << isEven
let isMultipleOfThree = isMultipleOf 3
let endsWithZero = isMultipleOf 10

F# Interactive would respond:

isEven : int -> bool
isOdd : int -> bool
isMultipleOfThree : int -> bool
endsWithZero : int -> bool

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:

isMultipleOf 10 150

This would return 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.

Actually, function application is left associative, which means that the line above should be interpreted as:

(isMultipleOf 10) 150

That is, you put the parenthesis around the leftmost function application.

Now, if you can understand all this, your example (which is the canonical CreateAdder) should be trivial!

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 - &gt; int - &gt; INT&QUOT;这在F#中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆