Why does the following match?

MatchQ[HoldPattern[f], HoldPattern[_]]


But the following does not:

MatchQ[HoldPattern[f], HoldPattern[f]]



MatchQ[HoldPattern[f[1]], HoldPattern[f[_]]]

  • 4
    $\begingroup$ To match an expression itself is a pattern, you'll need Verbatim to "inactivate" pattern expression to make it match, something like MatchQ[HoldPattern[f], Verbatim[HoldPattern[f]]]. $\endgroup$
    – Silvia
    Commented Nov 4, 2023 at 17:24
  • $\begingroup$ Thank you, Verbatim[HoldPattern[f]] is the pattern that works, my unsuccessful attempt was HoldPattern[HoldPattern]. $\endgroup$ Commented Nov 4, 2023 at 19:13

1 Answer 1


The first one matches because HoldPattern just prevents the pattern from evaluation, so it is equivalent to

MatchQ[HoldPattern[f], _]
(* True *)

The second one does not match because it is, again, equivalent to

MatchQ[HoldPattern[f], f]
(* False *)

What you are probably looking for is Verbatim.

MatchQ[HoldPattern[f], Verbatim[HoldPattern[f]]]
(* True *)
  • $\begingroup$ I still don't understand. The job of HoldPattern is to extract the pattern of an expression without actually evaluating it, in that sense f == _ , f == f and f[1]==f[ _ ] .There is still something I am missing. $\endgroup$ Commented Nov 4, 2023 at 20:58
  • 1
    $\begingroup$ @EhabShoubaki, yes, but in the code, it is not f compared to f, but instead HoldPattern[f] compared to f, which is obviously not the same. HoldPattern in the first argument does not have any special meaning, it is just this exact expression. $\endgroup$
    – Domen
    Commented Nov 4, 2023 at 21:38
  • 2
    $\begingroup$ This is good but I think the following variant might be preferrable. In[82]:= MatchQ[HoldPattern[f], Verbatim[HoldPattern][f]] Out[82]= True It makes no difference in this example, but there can be cases where it is only the HoldPattern that should be verbatimmed (to make up a term). $\endgroup$ Commented Nov 4, 2023 at 21:59
  • $\begingroup$ @DanielLichtblau, thanks for your valuable comment! Can you perhaps make a simple example so that I can include it in the answer? $\endgroup$
    – Domen
    Commented Nov 5, 2023 at 0:20
  • 1
    $\begingroup$ @Domen , so the way to understand this is that MatchQ expects an "expression" in the first argument, and HoldPattern would still be the head of that expression, while for the second argument it expects a "pattern", which it extracts from inside the HoldPattern unevaluated and discards HoldPattern head. Verbatim forces MatchQ to keep that head. $\endgroup$ Commented Nov 5, 2023 at 0:26

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.