Re: unbelievable ...
- To: mathgroup at smc.vnet.net
- Subject: [mg16614] Re: unbelievable ...
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Fri, 19 Mar 1999 12:53:46 -0500
- References: <7cnl3i$e6f@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
schatzi wrote in message <7cnl3i$e6f at smc.vnet.net>...
>Hello,
>
>I just came accross something that I *DO NOT* understand.
>
>Let A be a structure with two componants x and y and xA and yA functions
>that return its x and y
>componants.
>
>ClearAll[A,xA,yA]
>xA[A[x_,y_]]:=x
>yA[A[x_,y_]]:=y
>
>So far everything is fine ...
>
>{a=A[1,2],xA[a],yA[a]}
>=> {A[1,2],1,2}
>
>Now I would like to have a function which sets the x componant of a A
>symbol.
>I guess the following is not only far from the state of art, but deeply
>wrong:
>
>xASet[A[x_,y_],z_]:=(A[x,y]=A[z,y])
>
>The first use seems ok:
>
>{a,xASet[a,3],a}
>=> {A[1,2],Null,A[3,2]}
>
>But now it is impossible to reset a to A[1,2] (and only to A[1,2] !!!)
>
>{a=A[1,2],a=A[1,5]}
>=> {A[3,1],A[1,5]}
>
>
>Thank you very much for your help.
>
>
>antoine.zahnd at iprolink.ch
>
Antoine,
ClearAll["`*"]
Its all a matter of storing and calling rules:
You define a initial value for a and the function xASet
a=A[1,2];
xASet[A[x_,y_],z_]:=(A[x,y]=A[z,y])
Then you evaluate
{a,xASet[a,3],a}
{A[1,2], A[3,2], A[3,2]}
Your subsequent "odd behaviour" is related to how the last entry arises.
Let's look at the stored rules that give it.
?a
"Global`a"
a = A[1, 2]
So in fact a is still stored as A[1,2].
What your function xASet did was to add another rule that stores the value
A[3,2] for A[1,2]
?A
"Global`A"
A[1, 2] = A[3, 2]
It did not change the stored value (own value) of a.
But when the last entry in {a,xASet[a,3], a} was evaluated the steps were
a
A[1,2] (from the stored value for a)
A[3,2] ( from the stored value for A[1,2])
So, we do not need to reset a to A[1,2] - it is still set to this.
However, when we try to do this with
a=A[1,2]
the evaluation proceeds as follows
a = A]1,2]
--> a = A[3,2] ( with = the right side is evaluated before the rule is
stored)
this is stored
finally A[3,2] is output,
We have in fact stored a as A[3,2]
?a
"Global`a"
a = A[3, 2]
When a = A[1,5] is evaluated the steps are now
a = A[1,5]
this is stored
?a
"Global`a"
a = A[1, 5]
The value that you see as output may be different form the one stored,
because of the effect of other stored rules.
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565