简体   繁体   中英

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.

let min,subheap = findMin heap' in let rtn = root topnode

It seems that findMin heap' is self contained unit of execution. 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.

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. You have to look at that "Verbose Syntax (F#)" documentation.

The verbose syntax for let expressions is 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 . 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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