Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #2011

Re: thoughts on how to explain this functionality?

From Andrzej Kozlowski <akoz@mimuw.edu.pl>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: thoughts on how to explain this functionality?
Date 2011-05-01 10:22 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <ipjc9n$j2e$1@smc.vnet.net> (permalink)

Show all headers | View raw


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


Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: thoughts on how to explain this functionality? Andrzej Kozlowski <akoz@mimuw.edu.pl> - 2011-05-01 10:22 +0000

csiph-web