为什么这个F#函数需要括号? [英] Why are parentheses needed on this F# function?
问题描述
为什么在 read_rest_of_csv
下需要括号?
let read_rest_of_csv()=
csv_data.Add(csv_fileH.ReadFields())|>忽略
而不是csv_fileH.EndOfData
while read_rest_of_csv()do ignore None
打开系统
打开System.Threading
打开System.Collections.Generic
打开System.Linq
打开System.Text
打开System.Threading.Tasks
打开System.IO
打开Microsoft.VisualBasic。 FileIO
[< EntryPoint>]
let main argv =
let csv_fileH = new TextFieldParser(test1.csv)
csv_fileH.TextFieldType = FieldType.Delimited |>忽略
let x = csv_fileH.SetDelimiters(,)
let csv_data = new List< string []>()
let eod = csv_fileH.EndOfData
如果不是eod,则
让column_headings = csv_fileH.ReadFields()
csv_data.Add(column_headings)|>忽略
让read_rest_of_csv =
csv_data.Add(csv_fileH.ReadFields())|>忽略
而不是csv_fileH.EndOfData
,而read_rest_of_csv忽略无
0
我很抱歉,我不记得我在哪看到这个。我认为这是在SO。这是一个很好的例子。
难道这是没有parens我正在处理一个函数对象的类?
我的确不仅来自C,C ++和C#背景,还有一个中级Clojure背景。在我使用F#语法的情况下,更详细地阅读我的Haskell手册可能会有所帮助,因为它们的语法看起来很相似。
似乎来自C系列语言(C#,Java,C,C ++,JavaScript)的人在理解F#中括号的使用方面存在问题。我当然有,而且花了我几年的时间学习如何工作。
简而言之,F#中最基本的构建块是值。值可以是 let
-bound:
let foo = bar
这意味着 foo
是一个值,等于 bar
。
函数也是值:
//'a - > 'a *'a
let f = fun x - > x,x
这里, 写这个有点麻烦,所以有一个简写: 注意,没有括号在这些表达式中。 有时您需要调整运算符的优先级。就像在数学中一样, 不一样 (在这种情况下, 括号不表示函数调用;他们只是在那里控制评估的顺序。 有时候,你想要定义一个没有任何输入的函数。然而,你不能这样定义它: 因为这会使它成为一个值,它立即 这意味着 每当你用 Why are parentheses needed on Without the parentheses, the loop will not terminate. I apologize that I cannot remember where I saw this. I think it was in SO. It's a nice example. Could this be that without parens I'm dealing with a function object of sorts? I am indeed coming from not only a C, C++, and C# background, but also an intermediate Clojure background as well. In my case with F# syntax, reading my Haskell manual in a little more detail might have helped, because the syntaxes seem similar. It seems that people coming from C-family languages (C#, Java, C, C++, JavaScript) are having problems understanding the use of brackets in F#. I certainly had, and it took me some years learning how things work. In a nutshell, the most basic building block in F# is a value. Values can be This means that Functions are also values: Here, That's a bit cumbersome to write, so there's a shorthand for that: Notice that there are no brackets in these expressions. Sometimes you need to adjust the precedence of operators. Just like in maths, isn't the same as (in this case, Notice that the brackets don't denote a function call; they're only there to control order of evaluation. Sometimes, you want to define a function that doesn't take any input. You can't, however, define it like this: because that would make it a value that's immediately This means that Whenever you invoke 这篇关于为什么这个F#函数需要括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! f
是一个函数需要一些值( x
),并返回一个元组作为第一个元素和第二个元素<
//'a - > 'a *'a
让fx = x,x
1 + 2 * 3
(相当于 1 +(2 * 3)
)isn与(1 + 2)* 3
相同。在F#中,您还可以使用括号来覆盖优先级。因此
//'a - > string *'a
let fx = someOtherFunction x,x
// x:'a - >字符串
让fx = someOtherFunction(x,x)
someOtherFunction
是一个函数,它返回一个字符串
。)
let f = whatever
let
- 绑定到任何
。相反,您可以让函数获取内置类型 unit
的值。这种类型只有一个值,写成()
:
let f()= whatever
f
是模式匹配其输入与
()
调用 f
时,表达式 whatever
被评估并返回。read_rest_of_csv
below? let read_rest_of_csv() =
csv_data.Add(csv_fileH.ReadFields()) |> ignore
not csv_fileH.EndOfData
while read_rest_of_csv() do ignore None
open System
open System.Threading
open System.Collections.Generic
open System.Linq
open System.Text
open System.Threading.Tasks
open System.IO
open Microsoft.VisualBasic.FileIO
[<EntryPoint>]
let main argv =
let csv_fileH = new TextFieldParser("test1.csv")
csv_fileH.TextFieldType = FieldType.Delimited |> ignore
let x = csv_fileH.SetDelimiters(",")
let csv_data = new List<string[]>()
let eod = csv_fileH.EndOfData
if not eod then
let column_headings = csv_fileH.ReadFields()
csv_data.Add(column_headings) |> ignore
let read_rest_of_csv =
csv_data.Add(csv_fileH.ReadFields()) |> ignore
not csv_fileH.EndOfData
while read_rest_of_csv do ignore None
0
let
-bound:let foo = bar
foo
is a value, which happens to be equal to bar
.// 'a -> 'a * 'a
let f = fun x -> x, x
f
is a function that takes some value (x
) and returns a tuple with x
as both the first and the second element.// 'a -> 'a * 'a
let f x = x, x
1 + 2 * 3
(which is equivalent to 1 + (2 * 3)
) isn't the same as (1 + 2) * 3
. In F#, you also use brackets to override precedence. Thus// 'a -> string * 'a
let f x = someOtherFunction x, x
// x:'a -> string
let f x = someOtherFunction (x, x)
someOtherFunction
is a function that returns a string
.)let f = whatever
let
-bound to whatever
. Instead, you can let the function take a value of the built-in type unit
. This type only has a single value, which is written ()
:let f () = whatever
f
is a function that pattern matches its input against the only known value of unit
.f
with ()
, the expression whatever
is evaluated and returned.