带逗号的 F# let 语句的语义
我正在学习 F#。 我首先查看了 Microsoft 的 F# 示例。
我遇到了这样的语句:
let line1,line2 =
use sr = System.IO.File.OpenText @"test.txt"
let line1 = sr.ReadLine()
let line2 = sr.ReadLine()
(line1,line2)
有人能给我解释一下这个语句吗?
这里定义了什么类型? 一个函数? 一个元组?
为什么必须在 line1,line2
的定义中重新定义 line1
和 line2
(let line1 =
.. .让 line2 =
)?
最后一行 (line1, line2)
是怎么回事?它与原始定义的类型有何关系? 这是函数返回吗?
“语句”这个词在 F# 中用于定义是否正确?
谢谢。
I'm learning F#. I started by looking over the F# samples from Microsoft.
I ran across this statement:
let line1,line2 =
use sr = System.IO.File.OpenText @"test.txt"
let line1 = sr.ReadLine()
let line2 = sr.ReadLine()
(line1,line2)
Can anyone explain this statement to me?
What type is being defined here? A function? A tuple?
Why do line1
and line2
have to be redefined within the definition of line1,line2
(let line1 =
... let line2 =
)?
What's with the final line, (line1, line2)
and how does this tie into the type of the original definition? Is this the function return?
Is "statement" even the right word to use for a definition in F#?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
F# 中绑定标识符值的一般形式是
在这种情况下,模式是“line1, line2”,这是一个元组模式,它将期望绑定到值的 2 元组,并将名称“line1”和“line2”分配给这两个值。
表达式是接下来的 4 行。 该表达式内部有局部变量。 它们碰巧也被命名为“line1”和“line2”,但它们可以很容易地被重命名为“x”和“y”或其他任何名称——这些标识符的范围是这个缩进表达式的本地范围。 (对于编译器而言,使用与外部作用域中的名称相同的名称这一事实没有任何影响。)
最后一行 if 表达式是表达式的“返回值”。 在这种情况下,它返回值“line1”和“line2”的二元组(或者“x”和“y”,如果为了清楚说明而重命名它们)。 顺便说一句,由于这两个值的类型均为“string”,因此返回表达式的类型为“string*string”,这是一个二元组,其中每个值都是字符串。 这意味着第一行的原始“line1”和“line2”名称将被推断为“string”类型。
F# 是函数式的,因此从某种意义上说“一切都是表达式”并且“没有语句”(只有按顺序求值的表达式序列),但 IMO 可以(ab)使用术语“语句”来描述内部的“let”行,除非你想非常精确。
The general form for binding identifier values in F# is
In this case, the pattern is "line1, line2", which is a tuple pattern, it will expect to bind to a 2-tuple of values and assign the names "line1" and "line2" to those two values.
The expression is the next 4 lines. Inside that expression there are local variables. They happen to also be named "line1" and "line2", but they could easily have been renamed "x" and "y" or whatever - the scope of those identifiers is local to this indented expression. (The fact that the same names are used as the names in the outer scope has no effect as far as the compiler is concerned.)
The final line if the expression is the 'return value' of the expression. In this case it returns the 2-tuple of values "line1" and "line2" (or "x" and "y" if you rename them for clarity of exposition). Incidentally, since these two values each have type "string", the type of the return expression is "string*string", which is a 2-tuple where each value is a string. This means the original "line1" and "line2" names on the first line will each be inferred to have type "string".
F# is functional, and so in a sense "everything is an expression" and "there are no statements" (only sequences of expressions that are sequentially evaluated), but it is ok IMO to (ab)use the term "statement" to describe the inner "let" lines, unless you're trying to be very precise.