Lexical and dynamic scoping in Mathematica: Local variables with Module, With, and Block
- by dreeves
The following code returns 14 as you'd expect:
Block[{expr},
expr = 2 z;
f[z_] = expr;
f[7]]
But if you change that Block to a Module then it returns 2*z.
It seems to not matter what other variables besides expr you localize.
I thought I understood Module, Block, and With in Mathematica but I can't explain the difference in behavior between Module and Block in this example.
Related resources:
Tutorial on Modularity and the Naming of Things from the Mathematica documentation
Excerpt from a book by Paul R. Wellin, Richard J. Gaylord, and Samuel N. Kamin
Explanation from Dave Withoff on the Mathematica newsgroup