Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Possible Duplicate:
Why doesn't PatternTest work with Composition?

x := 2*3;
In[84]:= MatchQ[x,_?Function[Null,NumericQ@Unevaluated@#,HoldAll]]
Out[84]= False
In[83]:= MatchQ[x,_?(Function[Null,NumericQ@Unevaluated@#,HoldAll])]
Out[83]= True

Why the difference? Why the # is highlighted in front-end, indicating there is syntax error? What's the best way to use a Function[] in MatchQ[] to validate an expression by pattern matching its unevaluated form?

In[126]:= NumericQ[HoldAllComplete[2*3]]
Out[126]= False

What to put in <test> so the following return False?

MatchQ[x,_?<test>]
share|improve this question

marked as duplicate by rcollyer, Leonid Shifrin, FJRA, rm -rf, David Mar 7 '12 at 0:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
While it is a different function, the reasoning behind the behavior found in the link above is exactly the same. –  rcollyer Mar 6 '12 at 20:03
1  
@rcollyer I also voted to close, but still answered so that this question can be a gateway for users asking similar ones. –  Leonid Shifrin Mar 6 '12 at 20:04

1 Answer 1

up vote 2 down vote accepted

The difference is due to the tight binding of the PatternTest. Please see this question. The proper way is to use parentheses, as you did in your second line.

EDIT

To address the second part of the question (which was also requested in the comment below): if you don't want also your expression to evaluate, I'd use something like:

ClearAll[verbatimNumericQ];
SetAttributes[verbatimNumericQ,HoldAll];
verbatimNumericQ[expr_]:=
     MatchQ[Unevaluated[expr],_?(Function[Null,NumericQ@Unevaluated@#,HoldAll])]

Note, however, that, when applied to your x, while it would give False, this would be based on a literal x - it won't even get to the value stored in x.

share|improve this answer
    
The answer to the referred question doesn't answer all questions asked here. Using parentheses makes the output True. How to get a False result then? Because 2*3 while unevaluated is not NumericQ True. What's the best way to use a Function[] in MatchQ[] to validate an expression by pattern matching its "unevaluated" form? –  qazwsx Mar 6 '12 at 20:12
    
@Problemania I edited my answer - see if this answers your question. Note that, since you demand unevaluated form, it won't inspect the value of x (2*3). –  Leonid Shifrin Mar 6 '12 at 20:20
    
Is there a way to make verbatimeNumericQ effectively test 2*3 instead of literal x? In[187]:= MatchQ[Unevaluated[2+3],_?(Function[{x},NumericQ@Unevaluated@x,HoldAllComplete])‌​] Out[187]= False is correct. –  qazwsx Mar 6 '12 at 20:32
    
@Problemania If you pass 2*3 directly to verbatimNumericQ, it will do what is intended. If you store 2*3 in x, this is harder, because normally it is not possible to do the evaluation half-way (that is, in this case, evaluate x to 2*3, but prevent 2*3 from evaluating to 6). You would need some form of one-step evaluation, something like Unevaluated[verbatimNumericQ[x]]/.OwnValues[x], but at this point it starts feeling wrong. In most cases (with certain exceptions), a need for such constructs indicates some flaws in design. –  Leonid Shifrin Mar 6 '12 at 21:07

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