English 中文(简体)
F# - 《向浮游投放电动的规则》?
原标题:F# - Rules for casting integers to floats?
  • 时间:2024-03-25 15:44:18
  •  标签:
  • f#

我试图理解F#的类型推论制度,而且我不清楚其向浮动投放愤怒字面的政策。

这份汇编。 第3和第4条被解释为浮动。

let f (x: float) (y: float) = x + y
f 3 4

这并不汇编:

let f (x: float) = x + 3

对于我来说,ok,这种感觉似乎与那种推论制度的财产一样,即它可以同时推断,3个应当是浮动的,而<代码>+<>/code>应当是功能浮动――>浮动――>浮动。

但是,这确实汇编了:

let f (x: float) y = x + y

我很麻烦地把这些内容放在一个连贯的理论中,即推论的类型如何与决定字面的类型相互作用。 这里正在发生什么?

问题回答

类型推论是指未经您明确宣布汇编者将属于一类。 阁下

let f (x: float) y = x + y

the compiler infers y is a float, since you are adding it to x, which you have declared as a float. But with

let f (x: float) = x + 3

没有什么东西可以推断出3,它只是一个数字字面,在这种情况下是个ger。 如果是C#,你可以依靠汇编者,只是采取从编造到浮动的默示转变。 但是,F#并没有暗中转换,因为这会干扰类型的推论。 因此,让你在此保持不变:

let f (x: float) = x + 3.0

Or better yet

let f x = x + 3.0

由于现在编辑可以推断X是浮动。

its policy for casting integer literals to floats is not clear to me

我也不清楚。 不幸的是,我认为,这是公正的现实,是明确的。 这不是在F#中长期出现的情况,只是在最近才添加:rel=“nofollow noreferer”>。 此处指的是在F6号中添加该声明的通告,以及与《刑法》的联系。 并且正如评论中提到的“JL0PD”,

正如你在实例中指出的,这一特征半衰弱,我个人不同意在F#中添加这一内容。 我认为,这是对“Python”人群的姑息,但我认为,它使语言更加复杂,更不可预测。 事实上,现在我通常在而不是上依靠将 in化物转化为浮体。 它可能泄露到我的准则中,因为它的确使守则在发挥作用时更加简明,但这样,它就难以回避,只能被迫增加更多的说明或回馈和.0

因此,我认为答案是:

没有任何一般规则。 F#的汇编者将在能够暗中转换时尝试,但总是能够这样做。


造成这种情况的一些例子:

> [1;2;3.0];;

  [1;2;3.0];;
  -----^^^

stdin(1,6): error FS0001: All elements of a list must be implicitly convertible to the type of the first element, which here is  int . This element has type  float .

> let test: list<float> = [1;2;3];;
val test: float list = [1.0; 2.0; 3.0]

> [1.0;2;3.0];;
val it: float list = [1.0; 2.0; 3.0]

> [1.0;2;3];;
val it: float list = [1.0; 2.0; 3.0]

这完全令我感到困惑,因为在第一个案例中,我看不出为什么愤怒者可能被称为“被扩大”或被暗地转化为“一般”的<代码>float。 然而,看来它们并不是因为清单中的第一个要素是<条码>int。 但其他例子显示,如果<代码>float>首先出现,然后接着是ints,那么<代码>ints>就被默示转换。 它令人混淆和前后不一,当然!


If you want to understand the Hindley-Milner type inference algorithm, which is what F# s type system is based on to a degree, at a high-level, then I recommend the Programming Languages, Part A course on Coursera. That course uses Standard ML in Part A, and the instructor walks you through a couple of examples of how the type inference works.





相关问题
F#: Storing and mapping a list of functions

I have a number of events that happen in a game. I want to control the time and order at which these events occur. For example: Event 1: Show some text on screen for N frames & play a sound ...

Creating Silverlight 3 applications with F#

Is there an easy way to create Silverlight 3 applications with F# (October CTP)? I have seen the F# for Silverlight, but that only works with the May CTP. I am using Visual Studio Integrated Shell ...

How To Change List of Chars To String?

In F# I want to transform a list of chars into a string. Consider the following code: let lChars = [ a ; b ; c ] If I simply do lChars.ToString, I get "[ a ; b ; c ]". I m trying to get "abc". I ...

Unzipping ZLIB compressed portions of a binary file

I m reading a file(a flash swf) from .Net 3.5 that has a header which states whether the body of the file/Stream is compressed or not. However, I m having problems-after I rewrap the basic File ...

Pretty print a tree

Let s say I have a binary tree data structure defined as follows type a tree = | Node of a tree * a * a tree | Nil I have an instance of a tree as follows: let x = Node (Node (...

F# String Pattern-Matching with Wildcards

As part of a project I have assigned myself as a way of improving my knowledge of F# and functional programming in general, I am attempting to write a string pattern-matching algorithm from scratch ...

热门标签