Re: Single-step evaluation in Mathematica
- To: mathgroup at smc.vnet.net
- Subject: [mg81284] Re: [mg81235] Single-step evaluation in Mathematica
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Tue, 18 Sep 2007 00:46:06 -0400 (EDT)
- References: <200709170732.DAA15466@smc.vnet.net> <96D69218-954A-4705-8EA8-8B79B44D9BDA@mimuw.edu.pl>
On 17 Sep 2007, at 19:21, Andrzej Kozlowski wrote: > *This message was transferred with a trial version of CommuniGate > (tm) Pro* > > On 17 Sep 2007, at 16:32, Szabolcs Horv=E1t wrote: > >> >> I would like to have a function, hasOwnValue, that takes the name >> of a >> symbol as a string, and tells whether the symbol has an own- >> value. It >> should do this without evaluating the value of the symbol. >> >> Examples: >> >> abc := Print["side effect"] >> xyz =. >> >> In := hasOwnValue["abc"] (* "side effect" must not be printed *) >> Out = True >> >> In := hasOwnValue["xyz"] >> Out = False >> >> How can I achieve this? >> >> This is a practical problem that I ran into, but the general question >> is: What do you do when you feel the need for *single-step >> evaluation* >> in Mathematica? E.g. how can I manipulate/transform held expressions >> made up of symbols that have values? An artificial example: >> >> a = 1 >> Hold[{a,b,c}] >> >> How do I reverse the list inside Hold? I could map Hold to each >> element, reverse the list, hold the list again, and remove Hold from >> individual elements to get Hold[{c,b,a}], but this is very >> inconvenient. >> >> Szabolcs >> > > > There is of course the built in function ValueQ which essentially > does what you are askign for: > > abc := Print["side effect"] > ValueQ[abc] > True > > abc =. > ValueQ[abc] > False > > Of course the difference here is that the argument of VlaueQ is an > expression and not the string that is the name of the expression. > However, you can make it work with strings: > > SetAttributes[f, HoldAll] > f[x_String] := ValueQ @@ ToExpression[x, InputForm, Hold] > > Now: > > abc := Print["side effect"] > > f["abc"] > True > abc =. > f["abc"] > False > > Andrzej Kozlowski > > > I forgot about the second part of your question. I don't think it is possible to achiev ein Mathematica "signle step evaluation" of the kind you seem to be askign for. However, there are many ways to acheive the fect you desire. The one that I woudl choose involves working inside Block with the variables you do not want to evaluate localized, e.g.: a = 1; s = Hold[{a, b, c}]; Block[{a}, Hold @@ Reverse /@ List @@ s] Hold[{c, b, a}] Andrzej Kozlowski=
- References:
- Single-step evaluation in Mathematica
- From: Szabolcs Horvát <szhorvat@gmail.com>
- Single-step evaluation in Mathematica