Re: thoughts on how to explain this functionality?
- To: mathgroup at smc.vnet.net
- Subject: [mg118531] Re: thoughts on how to explain this functionality?
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Sun, 1 May 2011 06:22:35 -0400 (EDT)
On 30 Apr 2011, at 11:51, Scot T. Martin 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.
The point is that ValueQ has the HoldAll attribute, but your valueQ does not. You can give it the HoldAll attribute like this:
With[{valueQ =
Function[x, If[ValueQ[x], x, False], HoldAll]}, valueQ@x]
5
Note also that your ReleaseHold approach does not really work (it only seems so). Just compare what happen when you try it on a symbol y which has no value assigned to it:
With[{valueQ = If[ValueQ[ReleaseHold[Hold[#]]], #, False] &},
valueQ@y]
y
By contrast:
With[{valueQ =
Function[x, If[ValueQ[x], x, False], HoldAll]}, valueQ@y]
False
Your approach will never return False for it does not really do what you intended it to do (I think).
Andrzej Kozlowski