7
$\begingroup$

Consider these examples:

MatchQ[, _]                   (* True *)
MatchQ[{wald}, {_Symbol}]     (* True *)
MatchQ[5, _Integer]           (* True *)
MatchQ[{wald}, _{_Symbol}]    (* False *)

Why does Mathematica not see the fourth pattern as valid, namely as a pattern restricted to be a list consisting of a Symbol (or alternatively, as an empty expression followed by a list of a Symbol)?

I am using Mathematica 12.2.

$\endgroup$
2
  • 4
    $\begingroup$ Look at FullForm[_{_Symbol}]. I think it's different than what you think it is, maybe. $\endgroup$
    – Goofy
    Commented Nov 27, 2023 at 14:43
  • $\begingroup$ did you mean _[_Symbol]? $\endgroup$
    – Kuba
    Commented Nov 27, 2023 at 14:52

2 Answers 2

7
$\begingroup$

One source of confusion here is about what MatchQ[, _] actually does. Contrary to what you think, the first argument of MatchQ here is not "the empty expression" (which isn't a thing in WL) but it's Null.

Consider, for example:

{, 1}
Hold[, 1]

{Null, 1}

Hold[Null, 1]

Null is a special value that doesn't get printed when it's returned to the FE as the result of an evaluation, but it will get printed if it's part of an expression:

Null (* no output *)
{Null}

{Null}

So MatchQ[, _]is just MatchQ[Null, _], which is obviously True.

The closest thing you get to an "empty expression" is Sequence[], which disappears whenever it is enclosed by a head that does not have the SequenceHold attribute:

f[Sequence[]]
HoldComplete[Sequence[]]

f[]

HoldComplete[Sequence[]]

but on its own it's still a non-empty expression:

Sequence[]

Sequence[]

$\endgroup$
6
$\begingroup$

Your pattern is actually interpreted as a multiplication:

FullForm[_{_Symbol}]

(* List[Times[Blank, Blank[Symbol]]] *)

To match a list consisting of a Symbol, use List[_Symbol] as a pattern:

MatchQ[{wald}, List[_Symbol]]
(* True *)
$\endgroup$

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.