Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #8415 > unrolled thread
| Started by | James Stein <mathgroup@stein.org> |
|---|---|
| First post | 2012-02-09 10:40 +0000 |
| Last post | 2012-02-11 20:47 +0000 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
How to call 'Clear' from within a function? James Stein <mathgroup@stein.org> - 2012-02-09 10:40 +0000
Re: How to call 'Clear' from within a function? David Bailey <dave@removedbailey.co.uk> - 2012-02-10 10:54 +0000
Re: How to call 'Clear' from within a function? Yves Klett <yves.klett@googlemail.com> - 2012-02-10 11:01 +0000
Re: How to call 'Clear' from within a function? A Retey <awnl@gmx-topmail.de> - 2012-02-11 20:47 +0000
| From | James Stein <mathgroup@stein.org> |
|---|---|
| Date | 2012-02-09 10:40 +0000 |
| Subject | How to call 'Clear' from within a function? |
| Message-ID | <jh07qo$huj$1@smc.vnet.net> |
Before trying to encapsulate 'Clear' in a function, I try this: Clear [ x ] ; x x = 2 Clear [ ToString [ Unevaluated [ x ] ] ] ; x And I expect to get three lines of output: x, 2, and x But instead I get these three lines output: x, 2 and 2. What do I misunderstand, or need to know? I observe that a function CAN obtain the name of a symbol passed to it: ClearAll [ f ] ; SetAttributes [ f, HoldFirst ] ; f [ arg_ ] := ( Print [ Unevaluated [ arg ] ] ; Clear [ ToString [ Unevaluated [ arg ] ] ] ) ; x = 2; (* assign a value to 'x' *) f [ x ] ; (* 'f' can print the symbol 'x' ! *) x (* but 'x' still has value '2' ; the 'Clear' had no effect !? *) Similar problems with trying to call 'Set' from within a function....
[toc] | [next] | [standalone]
| From | David Bailey <dave@removedbailey.co.uk> |
|---|---|
| Date | 2012-02-10 10:54 +0000 |
| Message-ID | <jh2t0q$l9$1@smc.vnet.net> |
| In reply to | #8415 |
On 09/02/2012 10:40, James Stein wrote: > Before trying to encapsulate 'Clear' in a function, I try this: > > Clear [ x ] ; > x > x = 2 > Clear [ ToString [ Unevaluated [ x ] ] ] ; > x > > And I expect to get three lines of output: x, 2, and x > But instead I get these three lines output: x, 2 and 2. > > What do I misunderstand, or need to know? Well Clear has attributes HoldAll, so you would need: In[6]:= Clear [ x ] ; x x = 2 Clear [ Evaluate[ToString [ Unevaluated [ x ] ]] ] ; x Out[7]= x Out[8]= 2 Out[10]= x > > I observe that a function CAN obtain the name of a symbol passed to it: > > ClearAll [ f ] ; > SetAttributes [ f, HoldFirst ] ; > f [ arg_ ] := ( Print [ Unevaluated [ arg ] ] ; > Clear [ ToString [ Unevaluated [ arg ] ] ] ) ; > x = 2; (* assign a value to 'x' *) > f [ x ] ; (* 'f' can print the symbol 'x' ! *) > x (* but 'x' still has value '2' ; the 'Clear' had no effect !? *) > > Similar problems with trying to call 'Set' from within a function.... The same comment applies, but why not just put: Clear[arg] David Bailey http://www.dbaileyconsultancy.co.uk
[toc] | [prev] | [next] | [standalone]
| From | Yves Klett <yves.klett@googlemail.com> |
|---|---|
| Date | 2012-02-10 11:01 +0000 |
| Message-ID | <jh2tf7$sc$1@smc.vnet.net> |
| In reply to | #8415 |
This works just as expected if you simply use Clear[x]; instead of your ToString construct (which throws a warning anyway to tell you something is wrong). Regards, Yves Am 09.02.2012 11:40, schrieb James Stein: > Before trying to encapsulate 'Clear' in a function, I try this: > > Clear [ x ] ; > x > x = 2 > Clear [ ToString [ Unevaluated [ x ] ] ] ; > x > > And I expect to get three lines of output: x, 2, and x > But instead I get these three lines output: x, 2 and 2. > > What do I misunderstand, or need to know? > > I observe that a function CAN obtain the name of a symbol passed to it: > > ClearAll [ f ] ; > SetAttributes [ f, HoldFirst ] ; > f [ arg_ ] := ( Print [ Unevaluated [ arg ] ] ; > Clear [ ToString [ Unevaluated [ arg ] ] ] ) ; > x = 2; (* assign a value to 'x' *) > f [ x ] ; (* 'f' can print the symbol 'x' ! *) > x (* but 'x' still has value '2' ; the 'Clear' had no effect !? *) > > Similar problems with trying to call 'Set' from within a function....
[toc] | [prev] | [next] | [standalone]
| From | A Retey <awnl@gmx-topmail.de> |
|---|---|
| Date | 2012-02-11 20:47 +0000 |
| Message-ID | <jh6k5j$jrl$1@smc.vnet.net> |
| In reply to | #8415 |
Am 09.02.2012 11:40, schrieb James Stein: > Before trying to encapsulate 'Clear' in a function, I try this: > > Clear [ x ] ; > x > x = 2 > Clear [ ToString [ Unevaluated [ x ] ] ] ; > x > > And I expect to get three lines of output: x, 2, and x > But instead I get these three lines output: x, 2 and 2. > > What do I misunderstand, or need to know? > > I observe that a function CAN obtain the name of a symbol passed to it: > > ClearAll [ f ] ; > SetAttributes [ f, HoldFirst ] ; > f [ arg_ ] := ( Print [ Unevaluated [ arg ] ] ; > Clear [ ToString [ Unevaluated [ arg ] ] ] ) ; > x = 2; (* assign a value to 'x' *) > f [ x ] ; (* 'f' can print the symbol 'x' ! *) > x (* but 'x' still has value '2' ; the 'Clear' had no effect !? *) > > Similar problems with trying to call 'Set' from within a function.... Clear and Set have attributes HoldAll and HoldFirst respectively. What you can do is: Clear[Evaluate[ToString[Unevaluated[x]]]]; but when used within a function definition, things are even simpler: ClearAll[f]; SetAttributes[f, HoldFirst]; f[arg_] := (Print[Unevaluated[arg]]; Clear[arg]); should do what you want. Why you would want to do something like that? It sounds like you probably rather should use one of the existing ways to localize variables. Maybe you want to describe what you try to do... hth, albert
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web