How does Ocaml scope work?

I'm starting to learn Ocaml using a hickey book , and I'm stuck on exercise 3.4, part 9

let xx = x + 1 in x 2

The result of the operation 3, but I do not understand why?

+5
source share
1 answer

When you write let x x = ..., you define a function with a name xthat associates the name xwith its argument.

Since you used letinstead let rec, the function does not know its name, as far as you know, the only thing xworth knowing is the one that is passed as an argument.

Therefore, when you call a function with x 2, it associates a value 2with a name xand evaluates x+1, receiving 3as a result.

+7
source

All Articles