1
$\begingroup$
ClearAll[f1, f2]
f1[x_] := x*x
f2 = #*# &;

This produces the expected results:

ValueQ[f1]  (* False *)
ValueQ[f2]  (* True *)

I find this unexpected:

ValueQ /@ {f1, f2}  (* {False,False} *)

How can I understand the difference?

$\endgroup$
1
  • 3
    $\begingroup$ There is a documentation example about this; use ValueQ /@ Unevaluated[{f1, f2}]. $\endgroup$
    – ilian
    Jun 9, 2017 at 16:14

1 Answer 1

2
$\begingroup$

In ValueQ /@ {f1, f2} the expression {f1, f2} is evaluated before ValueQ is applied, therefore ValueQ never "sees" f2, only its value #*# & which itself does not have a value.

It is critical to understand the standard evaluation order in Mathematica or you shall be chasing many problems or surprises of this nature. Recommended reading:

Operator precedence is also critical unless you exclusively use bracketed notation; see:

$\endgroup$
1
  • $\begingroup$ Thanks. I forgot that ValueQ has the HoldFirst attribute, which should have been obvious. $\endgroup$
    – Alan
    Jun 9, 2017 at 17:58

Your Answer

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

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