My original problem is to delete all rules starts with a[1]
from SubValues[a]
For example,
{HoldPattern[a[1][2]] :> 1, HoldPattern[a[2][3]] :> 5,
HoldPattern[a[1][x_, y_]] :> x + y,
HoldPattern[a[2][x_, y_]] :> x - y, HoldPattern[a[2][x_]] :> x}
would become
{HoldPattern[a[2][3]] :> 5, HoldPattern[a[2][x_, y_]] :> x - y,
HoldPattern[a[2][x_]] :> x}
I tried to use patterns at first, but I found that when matching patterns with HoldPattern
head, results become unpredictable.
I found 2 general pattern matching results with HoldPattern
I don't understand (doc on HoldPattern
and related did not say anything on this):
1.MatchQ[HoldPattern[a[1]], HoldPattern[_[_]]]
is True
, but include some names, and MatchQ[HoldPattern[a[1]], HoldPattern[a[_]]]
is False
.
2.MatchQ[HoldPattern[a[1]], HoldPattern[_[_]]]
is True
, but MatchQ[HoldPattern[a[1][1]], HoldPattern[_[_][_]]]
is False
.
I solved my problem with
FilterRules[SubValues[a], _?(FreeQ[#, name] &)]
But I would still like to know about matching HoldPattern
expressions.
Verbatim
:MatchQ[HoldPattern[a[1]], Verbatim[HoldPattern][a[_]]]
will returnTrue
. $\endgroup$Verbatim
work with blanks, and I have no idea whatHoldPattern
did to the expression that caused this mismatch. $\endgroup$