Re: How to call 'Clear' from within a function?
- To: mathgroup at smc.vnet.net
- Subject: [mg124869] Re: How to call 'Clear' from within a function?
- From: David Bailey <dave at removedbailey.co.uk>
- Date: Fri, 10 Feb 2012 05:52:55 -0500 (EST)
- Delivered-to: email@example.com
- References: <firstname.lastname@example.org>
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:= Clear [ x ] ; x x = 2 Clear [ Evaluate[ToString [ Unevaluated [ x ] ]] ] ; x Out= x Out= 2 Out= 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