Re: Protecting from evaluation
- To: mathgroup at smc.vnet.net
- Subject: [mg14665] Re: Protecting from evaluation
- From: "David Bailey" <db at salford-software.com>
- Date: Sat, 7 Nov 1998 02:10:20 -0500
- Organization: Salford Software.com
- References: <71jl01$8m7@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
William B. Marks wrote in message <71jl01$8m7 at smc.vnet.net>... >To display variable names along with their values I can put them in >quotes first using > >quote[var_] := ToString[var] >SetAttributes[quote,HoldAllComplete]. > >Then this works >quote[Unevaluated @ cVWGL] >cVWGL >but this fails >quote[cVWGL] >20 >but since this fails >Unevaluated /@ {cVThGL, cVWGL} >{Unevaluated[900], Unevaluated[20.]} > >I can't see a way to protect members of a list of variables except by >putting quotes around them by hand. Any suggestions? > > > I can't see a way to protect members of a list of variables except by > putting quotes around them by hand. Any suggestions? First, your 'quote' function can be written so you do not need to wrap its argument in 'Unevaluated' each time you use it. The problem with the function as you wrote it is that although the argument is not evaluated on the call to 'quote', it is still evaluated when you call 'ToString' - so you are no further forward! The answer is either to temporarily remove its value, e.g. using the 'Block' construct: SetAttributes[quote,HoldAllComplete]; quote[var_] := Block[{var},ToString[var]] or to use HoldForm: SetAttributes[quote,HoldAllComplete]; quote[var_] := ToString[HoldForm[var]] When you write Unevaluated /@ {cVThGL, cVWGL} it is important to realise that the /@ operator invokes the 'Map' function, which evaluates its arguments..... The following function will apply 'quote' to a list without evaluating it: SetAttributes[listQuote,HoldFirst]; listQuote[x_]:=ReleaseHold[Map[quote,Hold[x],{2}]] As you can see, manipulating unevaluated expressions is rather subtle and I usually recommend that people avoid giving their algebraic variables explicit values. For example, instead of setting cVWGL to 20, you could save this value in a list of transformation rules (stored in a variable) and apply this whenever you needed to map from variables to values. David Bailey Salford Software db at salford-software.com