1

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.

Phillip Scott Givens
  • 5,256
  • 4
  • 32
  • 54
  • 1
    I think you can read `in` as a newline in this case, i.e. it would be equivalent if you replaced `in` with a linefeed + indentation. – ReyCharles Jul 30 '15 at 13:07
  • possible duplicate of [Meaning of keyword "in" in F#](http://stackoverflow.com/questions/546287/meaning-of-keyword-in-in-f) – Jwosty Jul 30 '15 at 18:53

1 Answers1

5

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 lets in a single line.

sepp2k
  • 363,768
  • 54
  • 674
  • 675