MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Disappearing variables

Julian Stoev wrote in message <6s8ean$ijn at>...
>On 28 Aug 1998, Allan Hayes wrote:
>|nobody at News.Dal.Ca wrote in message <6rr9u0$15b at>... |>I
>want to to define a variable inside a function, as follows: f:=x=123;
>|>Whenever I run f, x should be given the value of 123, and if x
>doesn't |>exist, it should be created. However: |>
>|>In[1]:= f:=x=123;
>|>Out[3]:=123        ** x exists
>|>Out[5]:=123  ** f seems to work
>|>Out[6]:=x     ** x doesn't exist anymore, even though f worked |>
>|>It seems like x is created when I initially define f, whereas since f
>is |>SetDelayed,  x should be created when I run f. |>Does anyone know
>why x is not created when I run f? |>
>|>Vilis Nams
>|Let's look at the assignments that are stored: |
>|f := x = 123
>|f := Removed["x"] = 123
>|Information::notfound: Symbol x not found." |
>|So, f still evaluates with output 123 but it does not restore the
>|assignment x=123 nor recreate x; and i is it allowed to assign
>|Remove["x]" = 123.
>Let me add something. If you try:
>In[1]:= SetDelayed[f,Set[x,123]]
>In[2]:= x
>Out[2]= x
>In[3]:= f
>Out[3]= 123
>In[4]:= x
>Out[4]= 123
>, you will find, that x has a value only after you check for f and this
>is logical, because when you check for f, it executes Set[x,123].
>The only problems I see are, that Removed[] is not mentioned in the
>Mathematica book and in the help. The info I found about it is In[5]:=
>"Removed[string] is printed to indicate a symbol that has been removed."
>Also it is not very clear how x got defined somehow if SetDelayed has
>attributes HoldAll. Even when I tried
>In[1]:= SetDelayed[f,HoldComplete[Set[x,123]]]
>I got
>In[2]:= x
>Out[2]= x
>--Julian Stoev


>Also it is not very clear how x got defined somehow if SetDelayed has
>attributes HoldAll

The following may help - there is more about this in Mathematica in
Education and Research, Volume 7, Number 1, pp 32-33. Note that
although hold-attributes hold evaluation they also allow us, or
Mathematica, to specify how to continue.


 (* store SetDelayed[f,Set[x,123]] (unevaluated -
 possible since SetDelayed has attribute HoldAll);
 output Null (not shown)

f := x = 123

(* f is replaced by Set[x,123] which is then evaluated with the results
    Set[x,123] is stored and the output is 123*)



x = 123

  • Prev by Date: Summing over like elements
  • Previous by thread: Re: Disappearing variables
  • Next by thread: Re: Derivative via mathematica