Group Abstract Group Abstract

Message Boards Message Boards

1
|
11K Views
|
12 Replies
|
9 Total Likes
View groups...
Share
Share this post:

Avoid unexpected behavior with ValueQ?

12 Replies
Posted 9 years ago
POSTED BY: Itai Seggev

Excellent - many thanks, Sander!

(As an off topic remark concerning readability: See this discussion).

Excellent - many thanks, Sander!

(As an off topic remark concerning readability: See this discussion).

POSTED BY: Henrik Schachner
POSTED BY: Sander Huisman

In principle - according to Sanders remark - Trace is already perfectly showing what is going on. One can improve the readability simply by wrapping the output in a "repeated frame" like so:

Framed //@ Trace[Table[If[ValueQ[f[i]], f[i], "No value"], {i, 2}]]

Regards -- Henrik

POSTED BY: Henrik Schachner

Sander Huisman, Tanks, this is also a good implementation. I tend to think that pure functions are the fastest way to proceed when writing algorithms... but that is more than a feeling than anything else.

It will keep in mind this alternative way of working things out.

Thanks again

Jesus

This is not a bug. ValueQ is a funny function and it's good to be aware what it actually does before attempting to use it. To say that it checks if the expression has a value is inaccurate and can be misleading.

ValueQ[x] will check the OwnValues of x is x is a symbol.

But in all other cases ValueQ simply evaluates its argument and checks if it has evaluated to something that differs from the original.

You can check the implementation:

ValueQ[expr_] := ! Hold[Evaluate[expr]] === Hold[expr]

This of course is problematic in so many ways, e.g. think about what happens if evaluating expr has side effects.

But this behaviour is clearly documented, see under Details in the doc page.

ValueQ gives False only if expr would not change if it were to be entered as Wolfram Language input.

There's a lot of discussion of ValueQ on StackExchange here:

POSTED BY: Szabolcs Horvát

As a side note: you could use an association to store your data:

Associations provide generalizations of symbolically indexed lists, associative arrays, dictionaries, hashmaps, structs, and a variety of other powerful data structures.

Then when you ask for a value that is not yet defined it will return missing:

hash = <||>;
Do[AssociateTo[hash, i -> i], {i, 1, 10, 2}]
Table[If[MissingQ[hash[i]], "No value", hash[i]], {i, 10}]

returns the correct result. This might even be a faster and more flexible solution.

POSTED BY: Sander Huisman

Thanks for the explanation. I find writing these things using Map easier anyhow. That's why I also suggested above to use Map over Table in such example.

I didn't know about the nested behaviour of these scopic constructs, thanks for showing.

POSTED BY: Sander Huisman

Thanks Guys

Your answers are all useful. Itai-- Thanks for your very precise explanation. It is educational. I am going to adopt 'map with a pure function' since it seems to me that it is more straightforward and easier to read.

Regards

Jesus

I noticed the same thing--either way, it is a bug.

Moderators, please take note and pass on to the developers.

POSTED BY: David Reiss

Hi Jesus,

This is indeed some 'unexpected' behavior, as an alternative you can use (2 methods):

If[ValueQ[f[#]], f[#], "No value"] & /@ Range[10]
Table[If[Head[f[i]] =!= f, f[i], "No value"], {i, 10}]

I think it has to do with ValueQ[f[i]] that is converted to:

!Hold[Evaluate[f[i]]]===Hold[f[i]]

I think the Hold interferes with Table, but I'm not 100% sure what the exact problem is. Reading a 'trace' is never easy:

Trace[Table[If[ValueQ[f[i]], f[i], "No value"], {i, 2}]]

gives:

{Table[If[ValueQ[f[i]],f[i],No value],{i,2}],{{ValueQ[f[i]],!Hold[Evaluate[f[i]]]===Hold[f[i]],{{{{i,1},f[1],1},Hold[1]},Hold[1]===Hold[f[i]],False},!False,True},If[True,f[i],No value],f[i],{i,1},f[1],1},{{ValueQ[f[i]],!Hold[Evaluate[f[i]]]===Hold[f[i]],{{{{i,2},f[2]},Hold[f[2]]},Hold[f[2]]===Hold[f[i]],False},!False,True},If[True,f[i],No value],f[i],{i,2},f[2]},{1,f[2]}}
POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard