Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1998
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1998

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

Search the Archive

Re: Disappearing variables

  • To: mathgroup at smc.vnet.net
  • Subject: [mg13853] Re: Disappearing variables
  • From: "Allan Hayes" <hay at haystack.demon.cc.uk>
  • Date: Mon, 31 Aug 1998 01:09:15 -0400
  • References: <6rr9u0$15b@smc.vnet.net> <6s5khr$c62@smc.vnet.net> <6s8ean$ijn@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Julian Stoev wrote in message <6s8ean$ijn at smc.vnet.net>...
>On 28 Aug 1998, Allan Hayes wrote:
>
>|nobody at News.Dal.Ca wrote in message <6rr9u0$15b at smc.vnet.net>... |>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;
>|>In[2]:=f
>|>Out[2]:=123
>|>In[3]:=x
>|>Out[3]:=123        ** x exists
>|>In[4]:=Remove[x];
>|>In[5]:=f
>|>Out[5]:=123  ** f seems to work
>|>In[6]:=x
>|>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
>|>
>|
>|Vilis,
>|Let's look at the assignments that are stored: |
>|f:=x=123;
>|
>|?f
>|
>|"Global`f"
>|f := x = 123
>|
>|Remove[x]
>|
>|?f
>|
>|"Global`f"
>|f := Removed["x"] = 123
>|
>|?x
>|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.
>|
>|Removed["x"]
>|
>|Removed["x"]
>
>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
>"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
>


Julian:

>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.

In[1]:=
Clear["`*"]

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

In[3]:=
?f
"Global`f"
f := x = 123


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

Out[4]=
123

In[5]:=
?x

"Global`x"
x = 123








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