Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #2016
| From | Stefan <wutchamacallit27@gmail.com> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: thoughts on how to explain this functionality? |
| Date | 2011-05-01 10:21 +0000 |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Message-ID | <ipjc71$j0b$1@smc.vnet.net> (permalink) |
| References | <ipgm48$8pt$1@smc.vnet.net> |
On Apr 30, 5:52 am, "Scot T. Martin" <smar...@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
Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Find similar | Unroll thread
thoughts on how to explain this functionality? "Scot T. Martin" <smartin@seas.harvard.edu> - 2011-04-30 09:52 +0000 Re: thoughts on how to explain this functionality? Stefan <wutchamacallit27@gmail.com> - 2011-05-01 10:21 +0000
csiph-web