3
$\begingroup$

If I start with a symbol q that I've assigned a value to,

q = 0;

and one, x, that I haven't, I get different answers from ValueQ depending on how I call ValueQ. If I map ValueQ over a list,

Map[ ValueQ, {q,x} ]

Mathematica returns {False, False}. If I apply ValueQ directly, however,

{ValueQ[q], ValueQ[x]}

Mathematica returns a different answer, {True, False}. I've tried

{Names["q"], Names["x"]}

which returns {{q},{}} before evaluating the two ValueQ calls, and {{q},{x}} after the calls. There's obviously some sort of side effect from the call to ValueQ taking place behind the scenes. If I quit the kernel to start with a clean slate each time, however, and reverse the order in which I call the two versions (Map or no Map) I get the same results.

This is troubling behavior. What's going on?

I'm using Mathematica 11.0.1.0 on Linux x86 (64-bit).

$\endgroup$
1
  • 1
    $\begingroup$ Please, see Section "Properties & Relations" in Documentation on ValueQ. The effect I suppose is concerned with HoldAll attribute of ValueQ, so that ValueQ/@Unevaluated[{q, x}] gives right answer {True, False}. $\endgroup$
    – Alx
    Dec 23, 2016 at 4:57

2 Answers 2

7
$\begingroup$

Map is evaluating its arguments, so you end up with ValueQ[0] (False) instead of ValueQ[q]] (True);

In[3]:= Map[ValueQ, Unevaluated[{q, x}]]

Out[3]= {True, False}
$\endgroup$
1
  • $\begingroup$ That makes sense now. I've seen other functions with HoldAll attribute behave the same way. $\endgroup$ Dec 24, 2016 at 1:35
3
$\begingroup$

There're actually 2 issues in your question. One is the evaluation control problem, this has been explained by Brett Champion and Alx. The other issue is:

Why {Names["q"], Names["x"]} returns {{"q"}, {}} before the ValueQ calls?

The answer is: ValueQ isn't relevant at all, the key point is, the symbol x has appeared in the code Map[ValueQ, {q, x}] so it's created.

A simpler way to reproduce the issue is:

Remove[x]
Names["x"]
(* {} *)
x;
Names["x"]
(* {"x"} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.