question is as follows
define a list
ttt={1,2};
and if I set values in this way
{ttt[[1]],ttt[[2]]}={3,4}
then the value of list ttt now becomes {3,4}
Now I try this
f:={ttt[[1]],ttt[[2]]}
then ?f will show that f:={tmp[[1]],tmp[[2]]}
then if I just write
f={3,4}
this will not affect the values of list ttt .
the question is that I want the values of list ttt changes as well when I set values to f. How to do this?
More Question!!
why the following code didn't change the value of ttt??
Table[With[{i = i}, Defer@ttt[[i]]], {i, 1, 2}] = {111, 222}
while
Table[With[{i = i}, Defer@ttt[[i]]], {i, 1, 2}]
really gives
{ttt[[1]],ttt[[2]]}
Answer
I would approach this by combining the functionality of bump given in:
Elegant manipulation of the variables list
with my step evaluation function described here:
How do I evaluate only one step of an expression?
The step function is needed to (easily) recover the expression {ttt[[1]], ttt[[2]]} from the definition of f without it fully evaluating. It can be used like this, with the injector pattern:
ttt = {1, 2};
f := {ttt[[1]], ttt[[2]]}
step[f] /. _[x_] :> (x = {3, 4})
ttt
{3, 4}
To make this more convenient, and to work with other functions (see the first question linked above for examples) I would then write (after loading step):
func_[boost[expr_], arg___] ^:= step[expr] /. _[x_] :> func[x, arg]
Example of use:
boost[f] = {5, 6};
ttt
{5, 6}
Comments
Post a Comment