Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #8442 > unrolled thread

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

Started byJames Stein <mathgroup@stein.org>
First post2012-02-10 10:56 +0000
Last post2012-02-10 10:56 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: How to call 'Clear' from within a function? James Stein <mathgroup@stein.org> - 2012-02-10 10:56 +0000

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

FromJames Stein <mathgroup@stein.org>
Date2012-02-10 10:56 +0000
SubjectRe: How to call 'Clear' from within a function?
Message-ID<jh2t5k$nu$1@smc.vnet.net>
Thanks for responding, Bob. Just three points:

(1) According to Mathematica's online documentation:

Clear [ "form-1", "form-2", ...]", clears values and definitions for
all symbols whose names match any of the string patterns form-i.

This suggests that Clear will "clear strings", as you put it, and so it does:

ClearAll [ f , x ];
f [ name_String ] := Clear [ name ] ;
x = 3 ;
f [ "x" ] ;
x (* now prints as "x, not "3" *)

(2) There are reasons to clear Strings rather than Symbols. A less
simple example of a function to Clear symbols might, for example, take
one or more string arguments from which it then constructs a list of
Symbols to be Cleared. For example, given "x", it might clear x2, x4,
x6, ... x100.

(3) I remain in the dark about why this example fails:

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' *) ;

It appears that Clear [ string ] works,
but Clear [ ToString [ Unevaluated [ symbol ] ] ] does not work.
Is a "String" not a "String Pattern" representing itself?
What mistake am I making?

-- James


On Thu, Feb 9, 2012 at 3:17 AM, Bob Hanlon <hanlonr357@gmail.com> wrote:
>
> Clear[x];
> x
> x = 2
> Clear[x] (* you clear symbols not strings *)
> x
>
>
> x
>
> 2
>
> x
>
> ClearAll[f];
> SetAttributes[f, HoldFirst];
> f[arg_] := (Print[Unevaluated[arg]]; Clear[arg]);
> x = 2
> f[x]
> x
>
> 2
>
> x
>
> x
>
>
> Bob Hanlon
>
> On Thu, Feb 9, 2012 at 5:38 AM, James Stein <mathgroup@stein.org> 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?
> >
> > 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] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web