Shadowing and Nested function
f#, shadowing
Solution
Your code is equivalent to the following, where I've simply numbered instances of your names to help you visualize how shadowing is occurring.
let func y0 =
let dup0 y1 = y1 + y1
let z0 = dup0 y0
let dup1 y2 =
let dup2 z1 =
let y3 = y2 * z1
y3
let z2 = y2
y2
dup1 z0 + z0
This can be further simplified, of course. Since `dup2` and `z2` are never used, `dup1` is equivalent to `let dup1 y2 = y2`, and the whole function is equivalent to
let func y0 =
let dup0 y1 = y1 + y1
let z0 = dup0 y0
dup1 z0 + z0
Which is equivalent to
let func y0 =
let z0 = y0 + y0
z0 + z0
by substitution. This is the same as
let func y0 = 4 * y0
Does this help?
Problem
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?