18 Variable Expression and Scope

Wait, how exactly are we defining x? We're essentially writing

let x = 42 in
    (* the rest of what we type in the toplevel *)

We're not re-declaring x, we're just declaring another x on top of it! The equivalent of

let x = 42 in
    let x = 7 in
        (* everything else *)

This is called overlapping scope. It's confusing, and here's another example:

It's a bit of a brain twister what this means, and while it does have a strict definition, it's annoying to think about. Let's avoid it in our programs.

Enter the principle of name irrelevance, a term invented by Prof. Clarkson. It says that the name of a variable shouldn't intrinsically matter.

E.g. in math, these are the same function:

They both add 1 to their argument.

To obey this rule, we need to stop substituting when we reach a binding of the same name.

See that unused variable warning? That's because we just dropped the value 5 for x when we reached the next let binding.