1
$\begingroup$

I was surprised by the following behavior of PatternTest and I think that this may be instructive for other people too.

Consider FreeQ[x], an operator that returns True if its operand does not contain x. E.g. FreeQ[x][1] returns True.

Now look at:

MatchQ[2 , _?FreeQ[x]]
False

Not what I ecpected. However, if we put brackets around FreeQ[x]:

MatchQ[2 , _?(FreeQ[x])]
True

With the help of Wolfram support I could finally entangle this mystery. The PatternTest has a pretty high precedence, higher than Part, so that _?FreeQ[x] is interpreted as (_?FreeQ)[x] and not as expected: _?(FreeQ[x])

$\endgroup$
7
  • 1
    $\begingroup$ What's the question you want answered? (Precedence is not a mysterious: multiple-click/extend selection shows the syntax structure. This is a recent Q&A on something similar, and Mr.Wizard's answer to its dupe mentions the issue above.) $\endgroup$
    – Michael E2
    Jun 17, 2021 at 20:40
  • 2
    $\begingroup$ Voted to close this as a duplicate, but also I have to note that a way to share some information here on this site, is to post a question and self-answer it, rather than simply lay down your findings directly in the question. $\endgroup$ Jun 17, 2021 at 21:22
  • $\begingroup$ @Leonid Shifrin Hi, I do not understand what is so bad in sharing experience. And to post a question and then at the same time answering it, seems more complicated than needed. Can you explain why this should be done? $\endgroup$ Jun 18, 2021 at 7:05
  • $\begingroup$ @DanielHuber [1/2] This is because of the format of this site. When you answer within your own question, several things go against the site Q / A model: a. There tends to be no clear question and answer part, but just some discussion b. Question will probably remain unanswered (when looked at it from the outside), while actually having an answer within question itself. c. This does not encourage others to submit alternative answers, which leads to poorer community experience with this Q/A, and lost opportunities for more complete coverage of the topic. ... $\endgroup$ Jun 18, 2021 at 9:26
  • $\begingroup$ @DanielHuber [2/2] d. People can not vote for question and the answers separately. Imagine someone who would like to vote for your question, but not for the answer you provided within it, since they don't agree with it or have a better alternative in mind. With a question self-answered within the question itself, they can't. $\endgroup$ Jun 18, 2021 at 9:28

0

Browse other questions tagged or ask your own question.