MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: ValueQ

  • To: mathgroup at smc.vnet.net
  • Subject: [mg34345] RE: [mg34327] ValueQ
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
  • Date: Thu, 16 May 2002 05:08:27 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

> -----Original Message-----
> From: rainer [mailto:rainer.gruber at gmx.at]
To: mathgroup at smc.vnet.net
> Sent: Wednesday, May 15, 2002 9:35 AM
> To: mathgroup at smc.vnet.net
> Subject: [mg34345] [mg34327] ValueQ
> 
> 
> Hi,
> 
> I#m searching for a simple workaround of the following behaviour.
> 
> For the symbol 'a' I've defined
> 
> In[1]:=
>    a[1] = 2;
> 
> When I evaluate ValueQ for a defined and for not a defined 
> expression I 
> get what I expect:
> 
> In[3]:=
>    ValueQ[a[1]]
> Out[3]=
>    True
> 
> In[4]:=
>    ValueQ[a[2]]
> Out[4]=
>    False
> 
> But when I evaluate ValueQ e. g. within a Table I always get True:
> 
> In[5]:=
>    Table[ValueQ[a[i]], {i, 1, 2}]
> Out[5]=
>    {True, True}
> 
> The 2nd 'True' is because 'a[i]' is not equal to 'a[2]'. A first 
> solution to get the expected result is
> 
> In[6]:=
>    Table[ToExpression@("ValueQ[a[" <> ToString[i] <> "]]"), {i, 1, 2}]
> Out[6]=
>    {True, False}
> 
> Does anybody know something better?
> 
> Rainer Gruber
> JOHANNES KEPLER UNIVERSITY LINZ
> Institute of Experimental Physics
> Atomic Physics and Surface Science
> 
> 

Rainer,

an easy way, I think, would be 

In[3]:= ValueQ[a[#]] & /@ Range[2]
Out[3]= {True, False}

ValueQ tests whether its argument changes when beeing evaluated. This Test
has to be done as early as possible in the evaluation sequence. Now Table
has a non-standard evaluation, so we must be very cautious to interprete the
result of ValueQ therein anyhow. A trace shows what happens: the ValueQ
machinery ist started before i gets its value and such it finally compares
(when the iterator reaches i -> 2) a[i]===a[2]. This is false and such it
conludes that a[i] has a value. It does, in this certain procedural sense --
and any sense is procedural with a computer! The problem is that it is
accross our expectations. 

You can however trick Table to do what want, just navigating away from that
dangerous cliff:

In[14]:= Table[Unevaluated[ValueQ[a[i]]] /. HoldPattern[i] -> i, {i, 1, 2}]
Out[14]= {True, False}

Here we forced the substitution for i before ValueQ could start, as above at
In[3] or as in here

In[23]:= Table[With[{i = i}, ValueQ[a[i]]], {i, 1, 2}]
Out[23]= {True, False}

In[25]:= Table[ValueQ[a[#]] &[i], {i, 1, 2}]
Out[25]= {True, False}

--
Hartmut



  • Prev by Date: Re: ValueQ
  • Next by Date: RE: RE: On Defining Functions Symmetric wrt Some Indices
  • Previous by thread: Re: ValueQ
  • Next by thread: RE: Re: ValueQ