简体   繁体   English

你什么时候在F#中加上双分号?

[英]When do you put double semicolons in F#?

This is a stupid question. 这是一个愚蠢的问题。 I've been reading a couple books on F# and can't find anything that explains when you put ;; 我一直在读F#上的几本书,找不到什么可以解释你放的时间;; after a statement, nor can I find a pattern in the reading. 在发表声明后,我也无法在阅读中找到模式。 When do you end a statement with double semi-colons? 你什么时候用双分号结束声明?

In the non-interactive F# code that's not supposed to be compatible with OCaml, you shouldn't need to ever need double semicolon. 在不应该与OCaml兼容的非交互式F#代码中,您不需要需要双分号。 In the OCaml compatible mode, you would use it at the end of a top-level function declaration (In the recent versions, you can switch to this mode by using files with .ml extension or by adding #light "off" to the top). 在OCaml兼容模式下,您将在顶级函数声明的末尾使用它(在最新版本中,您可以通过使用扩展名为.ml文件或将#light "off"添加到顶部来切换到此模式)。

If you're using the command-line fsi.exe tool or F# Interactive in Visual Studio then you'd use ;; 如果您在Visual Studio中使用命令行fsi.exe工具或F#Interactive,那么您将使用;; to end the current input for F#. 结束F#的当前输入。

When I'm posting code samples here at StackOverflow (and in the code samples from my book ), I use ;; 当我在StackOverflow上发布代码示例时(以及我书中的代码示例),我使用;; in the listing when I also want to show the result of evaluating the expression in F# interactive: 在列表中我还想显示在F#interactive中评估表达式的结果:

  • Listing from F# interactive 从F#互动列表

     > "Hello" + " world!";; val it : string = "Hello world!" > 1 + 2;; val it : int = 3 
  • Standard F# source code 标准F#源代码

     let n = 1 + 2 printf "Hello world!" 

Sometimes it is also useful to show the output as part of the listing, so I find this notation quite useful, but I never explained it anywhere, so it's great that you asked! 有时将输出显示为列表的一部分也很有用,所以我觉得这个符号非常有用,但我从来没有在任何地方解释过,所以你问的很好!

Are you talking about F# proper or about running F# functions in the F# Interactive? 您是在谈论F#本身还是在F#Interactive中运行F#函数? In F# Interactive ;; 在F#Interactive ;; forces execution of the code just entered. 强制执行刚输入的代码。 other than that ;; 除此之外 ;; does not have any special meaning that I know of 没有任何我所知道的特殊含义

In F#, the only place ;; 在F#,唯一的地方;; is required is to end expressions in the interactive mode. 需要的是以交互模式结束表达式。

;; is left over from the transition from OCaml, where in turn it is left over from Caml Light. 从OCaml的过渡中遗留下来,而OCaml又从Caml Light中遗留下来。 Originally ;; 最初;; was used to end top-level "phrases"--that is, let , type , etc. OCaml made ;; 用于结束顶级“短语” - 即, lettype等.OCaml制作;; optional since the typical module consists of a series of let statements with maybe one statement at the end to call the main function. 是可选的,因为典型的模块由一系列let语句组成,最后可能有一个语句来调用main函数。 If you deviate from this pattern, you need to separate the statements with ;; 如果偏离此模式,则需要将语句与;; . Unfortunately, in OCaml, when ;; 不幸的是,在OCaml中,当;; is optional versus required is hard to learn. 是可选的与需要的是很难学习。

However, F# introduces two relevant modifications to OCaml syntax: indentation and do . 但是,F#引入了对OCaml语法的两个相关修改:indentation和do Top-level statements have to go inside a do block, and indentation is required for blocks, so F# always knows that each top-level statement begin with do and an indent and ends with an outdent. 顶级语句必须进入do块,并且块需要缩进,因此F#始终知道每个顶级语句以do开头并以缩进开头并以outdent结束。 No more ;; 不再;; required. 需要。

Overall, all you need to know is that [O']Caml's syntax sucks, and F# fixes a lot of its problems, but maintains a lot of confusing backward compatibility. 总的来说,你需要知道的是[O'] Caml的语法很糟糕,而且F#修复了很多问题,但是仍然存在许多令人困惑的向后兼容性。 (I believe that F# can still compile a lot of OCaml code.) (我相信F#仍然可以编译很多OCaml代码。)

Note: This answer was based on my experience with OCaml and the link Adam Gent posted (which is unfortunately not very enlightening unless you know OCaml). 注意:这个答案是基于我对OCaml的经验以及Adam Gent发布的链接 (不幸的是,除非你知道OCaml,否则不是很有启发性)。

Symbol and Operator Reference (F#) 符号和运算符参考(F#)

http://msdn.microsoft.com/en-us/library/dd233228(v=VS.100).aspx http://msdn.microsoft.com/en-us/library/dd233228(v=VS.100).aspx

Semi Colon: 半冒号:

•Separates expressions (used mostly in verbose syntax). •分隔表达式(主要用于详细语法)。

•Separates elements of a list. •分隔列表的元素。

•Separates fields of a record. •分隔记录的字段。

Double Semi Colon: Double Semi Colon:

http://www.ffconsultancy.com/products/fsharp_journal/free/introduction.html http://www.ffconsultancy.com/products/fsharp_journal/free/introduction.html

Articles in The F#.NET Journal quote F# code as it would appear in an interactive session. F#.NET日记中的文章引用F#代码,因为它将出现在交互式会话中。 Specifically, the interactive session provides a > prompt, requires a double semicolon ;; 具体来说,交互式会话提供>提示,需要双分号; identifier at the end of a code snippet to force evaluation, and returns the names (if any) and types of resulting definitions and values. 用于强制评估的代码段末尾的标识符,并返回结果定义和值的名称(如果有)和类型。

I suspect that you have seen F# code written when #light syntax wasn't enabled by default (#light syntax is on by default for the May 2009 CTP and later ones as well as for Visual Studio 2010) and then ;; 我怀疑您在默认情况下未启用#light语法时已经看到了编写的F#代码(默认情况下,2009年5月的CTP以及之后的版本和Visual Studio 2010都启用了#light语法)然后;; means the end of a function declaration. 表示函数声明的结束。

So what is #light syntax? 那么#light语法是什么? It comes with the #light declaration: 它附带#light声明:

The #light declaration makes whitespace significant. #light声明使空白显着。 Allowing the developer to omit certain keywords such as in, ;, ;;, begin, and end. 允许开发人员省略某些关键字,例如in,;,;;,begin和end。

Here's a code written without #light syntax: 这是一个没有#light语法编写的代码:

let halfWay a b =
  let dif =  b - a in
  let mid = dif / 2 in
  mid + a;;

and becomes with light syntax: 并使用轻型语法:

#light
let halfWay a b =
  let dif =  b - a
  let mid = dif / 2
  mid + a

As said you can omit the #light declaration now (which should be the case if you're on a recent CTP or Visual Studio 2010). 如上所述,您现在可以省略#light声明(如果您使用的是最近的CTP或Visual Studio 2010,则应该如此)。

See also this thread if you want know more on the #light syntax: F# - Should I learn with or without #light? 如果你想了解更多关于#light语法的信息,请参阅这个主题: F# - 我应该学习有没有#light?

The double semi-colon is used to mark the end of a block of code that is ready for evaluation in F# interactive when you are typing directly into the interactive session. 当您直接在交互式会话中键入时,双分号用于标记可在F#interactive中进行评估的代码块的结尾。 For example, when using it as a calculator. 例如,将其用作计算器时。

This is rarely seen in F# because you typically write code into a script file, highlight it and use ALT+ENTER to have it evaluated, with Visual Studio effectively injecting the ;; 这在F#中很少见,因为您通常将代码写入脚本文件,突出显示它并使用ALT + ENTER进行评估,Visual Studio有效地注入了;; at the end for you. 最后为你。

OCaml is the same. OCaml是一样的。

Literature often quotes code written as it would appear if it had been typed into an interactive session because this is a clear way to convey not only the code but also its inferred type. 文献经常引用编写的代码,如果它已被输入到交互式会话中,因为这是一种明确的方式,不仅传达代码而且传达其推断类型。 For example: 例如:

> [1; 2; 3];;
val it : int list = [1; 2; 3]

This means that you type the expression [1; 2; 3] 这意味着您键入表达式[1; 2; 3] [1; 2; 3] [1; 2; 3] into the interactive session followed by the ;; [1; 2; 3]进入交互式会话,然后是;; denoting the end of a block of code that is ready to be evaluated interactively and the compiler replies with val it : int list = [1; 2; 3] 表示准备以交互方式进行评估的代码块的结尾,编译器回复val it : int list = [1; 2; 3] val it : int list = [1; 2; 3] val it : int list = [1; 2; 3] describing that the expression evaluated to a value of the type int list . val it : int list = [1; 2; 3]描述表达式求值为int list类型的值。

There is no purpose for double semi-colons (outside of F# interactive). 双重冒号没有任何目的(在F#互动之外)。 The semi-colon, according to MSDN : 根据MSDN的分号:

  • Separates expressions (used mostly in verbose syntax). 分隔表达式(主要用于详细语法)。
  • Separates elements of a list. 分隔列表的元素。
  • Separates fields of a record. 分隔记录的字段。

Therefore, in the first instance, ;; 因此,在第一种情况下, ;; would be separating the expression before the first semi-colon from the empty expression after it but before the second semi-colon, and separating that empty expression from whatever came after the second semi-colon (just as in, say C# or C++). 将第一个分号前的表达式与第二个分号之前的空表达式分开,然后将该空表达式与第二个分号后的任何表达式分开(就像在C#或C ++中一样)。

In the instance of the list, I suspect you'd get an error for defining an empty list element. 在列表的实例中,我怀疑您在定义空列表元素时会收到错误。

With regards to the record, I suspect it would be similar to separating expressions, with the empty space between the semi-colons effectively being ignored. 关于记录,我怀疑它与分离表达式类似,半冒号之间的空白空间实际上被忽略了。

F# interactive executes the entered F# on seeing a double semi-colon. F#interactive在看到双分号时执行输入的F#。

[Updated to cover F# interactive - courtesy of mfeingold ) [更新以涵盖F#互动 - 由mfeingold提供

The double semicolon most likely comes from OCaml since that is what the language is based on. 双分号最有可能来自OCaml,因为这是该语言的基础。 See link text 请参阅链接文字

Basically its for historical purposes and you need it for the evaluator (repl) if you use it. 基本上它用于历史目的,如果你使用它,你需要它为评估者 (repl)。

The history of the double semicolon can be traced back to the beginnings of ML when semicolons were used as a separator in lists instead of commas. 双分号的历史可以追溯到ML的开头,当分号用作列表中的分隔符而不是逗号时。 In this ICFP 2010 - Tribute to Robin Milner video around 50:15 Mike Gordon mentions: 在这个ICFP 2010中 -在50:15左右向Robin Milner致敬视频Mike Gordon提到:

There was a talk on F# where someone asked "Why is there double semicolon on the end of F# commands?" 在F#上有人谈到有人问“为什么F#命令结束时会有双分号?” The reason is the separator in lists in the original ML is semicolons, so if you wanted a list 1;2;3; 原因是原始ML中列表中的分隔符是分号,所以如果你想要一个列表1; 2; 3; and put it on separate lines- if you ended a line with semicolon you were not ending the phrase, so using double semicolon meant the end of the expression. 并将它放在单独的行 - 如果你用分号结束一行你没有结束短语,所以使用双分号意味着表达式的结束。 Then in Standard ML the separator for lists became comma, so that meant you could use single semicolons to end lists. 然后在标准ML中,列表的分隔符变为逗号,这意味着您可以使用单个分号来结束列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM