I want to understand how the mechanism of Shadowing and Nested function work.
For example:
let func y =
let dup y = y + y
let z = dup y
let dup y =
let dup z =
let y = y * z
y
let z = y
y
dup z + z;;
val func : int -> int
> func 3;;
val it : int = 12
Can someone explain what happen here?
Your code is equivalent to the following, where I’ve simply numbered instances of your names to help you visualize how shadowing is occurring.
This can be further simplified, of course. Since
dup2andz2are never used,dup1is equivalent tolet dup1 y2 = y2, and the whole function is equivalent toWhich is equivalent to
by substitution. This is the same as
Does this help?