Re: thoughts on how to explain this functionality?
- To: mathgroup at smc.vnet.net
- Subject: [mg118523] Re: thoughts on how to explain this functionality?
- From: Stefan <wutchamacallit27 at gmail.com>
- Date: Sun, 1 May 2011 06:21:09 -0400 (EDT)
- References: <ipgm48$8pt$1@smc.vnet.net>
On Apr 30, 5:52 am, "Scot T. Martin" <smar... at seas.harvard.edu> wrote: > In[1]:= x = 5 > > Out[1]= 5 > > In[2]:= With[{valueQ = If[ValueQ[ReleaseHold[Hold[#]]], #, False] &}, valueQ@x] > > Out[2]= 5 > > In[3]:= With[{valueQ = If[ValueQ[#], #, False] &}, valueQ@x] > > Out[3]= False > > Anyone have thoughts on why Out[2] and Out[3] are different? > > It seems to me that a ReleaseHold[Hold[...]] formulation would net out to no effect, yet the effect is apparent. > > My desired output is Out[2] but the formulation of ReleaseHold[Hold[...]] is not elegant. Scot, Ive never looked this closely at ValueQ, but it seems to have an interesting way of evaluating (it effectively checks ! Hold[Evaluate[#]] === Hold[#]). One thing to note is that ValueQ has the HoldAll attribute, so Hold[ValueQ[#]] actually changes nothing, then ReleaseHold actually is what changes it. The documentation for ValueQ says this and then shows a few lines which I think are relate to your problem. I've copied them below. ValueQ is HoldAll: In[1]:= x=y; In[2]:= {ValueQ[x],ValueQ[y]} Out[2]= {True,False} so above, x has a value, which is y. but y has no value. Here x is evaluated before ValueQ sees it: In[3]:= ValueQ /@ {x, y} Out[3]= {False, False} this is the problem you are having, it is not the symbol x which is passed to your function, but the evaluated version of x (=5), so you get False Use Unevaluated to preserve the HoldAll attribute: In[4]:= ValueQ /@ Unevaluated[{x, y}] Out[4]= {True, False} Hope this helps! -Stefan Salanski