[Date Index]
[Thread Index]
[Author Index]
Re: "dereference" variable
*To*: mathgroup at smc.vnet.net
*Subject*: [mg82194] Re: "dereference" variable
*From*: "Ted Ersek" <ted.ersek at tqci.net>
*Date*: Sun, 14 Oct 2007 06:16:02 -0400 (EDT)
With, Module, BlockTodd Johnson posted a question at:
http://forums.wolfram.com/mathgroup/archive/2007/Oct/msg00395.html
The example he provided was convoluted.
Instead I will give simple examples that might show how to solve his problem.
------------------------------------
Module creates local variables which are given local values. The local variables take on the specified values only after they evaluate. In the following example Hold prevents the local variable from evaluating, and this is normally not the desired result. In the output below the local variable is (s$5).
In[1]:= M1[x_]:= Module[{s=x+3}, Hold[x,s^2] ];
M1[z]
Out[2]= Hold[z, s$5+4]
With[{s=val},expr] replaces each instances of (s) in (expr) with (val). This replacement is made before (expr) has a chance to evaluate. I think Todd Johnson's problem will be solved if he uses With instead of Module.
In[3]:= M2[x_]:= With[{s=x+3}, Hold[x,s^2] ];
M2[z]
Out[4]= Hold[z,(3+z)+4]
Note: When using With[{s=val}, expr] the value of (s) is (val) inside the With statement and you can't change it inside the With statement. On the other hand when using Module[{x,y}, expr] you can change the values assigned local variables (x,y) as much as you want during evaluation of (expr).
------------------------------------
Then there is Block[{x=0,y=y0}, expr] which evaluates (expr) with temporary values assigned to x, and y. You can also use Block[{x,y}, expr ] to evaluate (expr) with no values assigned to x, and y. Another thing about Block that isn't documented is that it temporarily clears any attributes of the affected symbols. It clears the attributes even if they were Protected (you don't need to Unprotect they symbols first).
So for example in the next line the pattern matches. This is because Plus has the attributes Flat and Orderless. These attributes allow the pattern matcher to regard Plus[x,b,z] as Plus[b,Plus[x,z]].
In[5]:= MatchQ[Plus[x,b,z],Plus[b,_]]Out[5]= True
In the next line the pattern doesn't match because Block supresses all attributes of Plus.
In[6]:= Block[{Plus}, MatchQ[Plus[x,b,z],Plus[b,_]]]
Out[6]= False
----------------------------------------
Alos in the next line we give (foo) the Orderless attribute inside Block. You will notice (foo) is only Orderless inside the Block.
In[7]:= Block[{foo},Attributes[foo]={Orderless};foo[z,a,x]]
Out[7]= foo[a,x,z]
----------------------------------------
For more on this stuff see:
http://reference.wolfram.com/mathematica/ref/With.html
http://reference.wolfram.com/mathematica/ref/Module.html
http://reference.wolfram.com/mathematica/ref/Block.html
http://reference.wolfram.com/mathematica/tutorial/ModulesAndLocalVariables.html
http://reference.wolfram.com/mathematica/tutorial/BlocksComparedWithModules.html
-----------------------------------------------------
You could also open my Tricks notebook at
http://library.wolfram.com/infocenter/MathSource/4557/
And see my sections on Evaluate and Unevaluated.
------------------------------------------------------
For more on working with unevaluated expressions see the notebook at
http://library.wolfram.com/infocenter/Conferences/377/
But beware that the syntax of some features such as ReplacePart is different in Mathematica 6 than it was when that notebook was written.
---------------------------------------------
Regards,
Ted Ersek
Prev by Date:
**Re: Re: What is the purpose of the Defer Command?**
Next by Date:
**Re: Compile forgets assigned values inside Do unless they are initialised**
Previous by thread:
**Re: "dereference" variable**
Next by thread:
**Typeset subscript on a operator**
| |