3

Based on the anwser of "Meaning of keyword “in” in F#":

let (x = 2 and y = x + 2) in
    y + x

This will not work the same as

let (x = 2 and y = x + 2)
    y + x

In the former case x is only bound after the in keyword. In the later case normal variable scoping rules take effect, so variables are bound as soon as they are declared.

When one need to specify bound variables with in, instead of binding as they are declared?

MiP
  • 5,846
  • 3
  • 26
  • 41
  • That is not even valid F#. `let` allows only one binding. And, as far as I understand, there is always `in`, but *Lightweight Syntax* allows you to omit it. – user4003407 Dec 18 '17 at 08:47
  • @PetSerAl it should have been `let x = 2 in y = x + 2 in y +x`, I just copied the accepted answer. – MiP Dec 18 '17 at 09:08
  • To be syntactically correct it should be `let x = 2 in let y = x + 2 in y + x`. – user4003407 Dec 18 '17 at 09:20

1 Answers1

5

You could use let/in like this:

let x = 2 in
    let y = x + 2 in
        y + x

F# is an expression-based language, and the code in this form reveals that. However, there is syntactic sugar built in so that you can write the same thing in a flat way that looks statement-based, but isn't really:

let x = 2
let y = x + 2
y + x

You could use in if you really want to stay on the same line let x = 2 in x + 2. However, it's very unusual to see this. In the thousands of lines of F# I've worked with I've never seen it or used it myself.

TheQuickBrownFox
  • 10,544
  • 1
  • 22
  • 35
  • What would you do if `x` and `y` are mutually dependent? – leftaroundabout Dec 18 '17 at 13:36
  • @leftaroundabout These are just values. If they were mutually dependent wouldn't it create an infinite loop to evaluate them? If you want mutually recursive functions then you can use [`let rec`...`and`](https://stackoverflow.com/questions/3621043/f-mutually-recursive-functions) – TheQuickBrownFox Dec 18 '17 at 13:46