Lexical and dynamic scoping in Mathematica: Local variables with Module, With, and Block

Posted by dreeves on Stack Overflow See other posts from Stack Overflow or by dreeves
Published on 2010-04-28T21:23:28Z Indexed on 2010/04/28 21:27 UTC
Read the original article Hit count: 358

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:

© Stack Overflow or respective owner

Related posts about mathematica

Related posts about module