[英]f#: Strange syntax in example using ".. in let .."
I am reading through an example on RosettaCode and do not exactly know what the following line is doing.我正在阅读有关RosettaCode的示例,但并不完全知道以下行在做什么。
let min,subheap = findMin heap' in let rtn = root topnode
It seems that findMin heap'
is self contained unit of execution.似乎findMin heap'
是自包含的执行单元。 I do not know how it relates to the "in" operator nor do I understand the use of the let statement in the "in" operator.我不知道它与“in”运算符的关系,也不了解“in”运算符中 let 语句的使用。
Here is the whole method这是整个方法
let rec private findMin heap =
match heap with | [] -> raise Empty_Heap //guarded so should never happen
| [node] -> root node,[]
| topnode::heap' ->
let min,subheap = findMin heap' in let rtn = root topnode
match subheap with
| [] -> if rtn.k > min.k then min,[] else rtn,[]
| minnode::heap'' ->
let rmn = root minnode
if rtn.k <= rmn.k then rtn,heap
else rmn,minnode::topnode::heap''
[Edit] Even after sepp2k explained it, the documentation for "let" does not explain this. [编辑] 即使在 sepp2k 解释了它之后,“let”的文档也没有解释这一点。 You have to look at that "Verbose Syntax (F#)" documentation.您必须查看“详细语法(F#)”文档。
The verbose syntax for let
expressions is let <ident> = <exp> in <exp>
. let
表达式的详细语法是let <ident> = <exp> in <exp>
。 So to define and add two variables you could write let x = 23 in let y = 42 in x + y
.因此,要定义和添加两个变量,您可以let x = 23 in let y = 42 in x + y
编写let x = 23 in let y = 42 in x + y
。 The light syntax of F# allows you to leave out the in
at the end of a line, so this example would instead be written as: F# 的轻量级语法允许您在行尾省略in
,因此此示例将改写为:
let x = 23
let y = 42
x + y
Your code mixed the verbose and the light syntax by keeping the first in
to be able to have the two let
s in a single line.您的代码通过保留第一个in
以便能够在一行中包含两个let
,从而混合了冗长和简洁的语法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.