0
votes

let p = let x = 1 in x + 1, let y = 2 in y + 1, 4

Since comma , have the lowest precedence, I would image p has 3 elements: (2, 3, 4).

But in fact, p has only 2 elements: (2, (3, 4))

Why?

Why the last , belongs to let y expression, but not outside of it?

2

2 Answers

3
votes

I would expect let...in... to have the following syntax

let binding = expression in expression

and the block goes as far to the right as possible.

In your example, the OCaml parser expects

let y = 2 in y + 1, 4

to be an expression and parses it as (3, 4) successfully.

An equivalent of your example with explicit brackets is

let p = (let x = 1 in x + 1, (let y = 2 in y + 1, 4))

If you would like to return final result (2, 3, 4), you should put a bracket to stop let...in... block in the appropriate place:

let p = let x = 1 in x + 1, (let y = 2 in y + 1), 4
0
votes

Try writing out on multiple lines, with indentation to show the relationship:

let p = 
    let x = 1 in x + 1, 
        let y = 2 in y + 1, 4

So you can see how one let "belongs" to another.

(2, (3,4)) is exactly what I'd expect.