Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2012

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

Search the Archive

Re: How to call 'Clear' from within a function?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg124897] Re: How to call 'Clear' from within a function?
  • From: A Retey <awnl at gmx-topmail.de>
  • Date: Sat, 11 Feb 2012 06:36:54 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <jh07qo$huj$1@smc.vnet.net>

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



  • Prev by Date: Re: MultipleListPlot
  • Next by Date: Need help with prime Test
  • Previous by thread: Re: How to call 'Clear' from within a function?
  • Next by thread: ListInterpolate and missing values