Skip to main content
deleted 1 character in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

Clarification of HoldPattern usage:

HoldPattern[expr] is equivalent to expr for pattern matching, but maintains expr in an unevaluated form.

I think this can be ambigiousambiguous for begginersbeginners. One could think that what we are doing in

MatchQ[HoldPattern[a[1]], HoldPattern[_[_]]] 

is to more or less

MatchQ[ a[1], _[_] ] 

where both arguments are kept unevaluated. That's not the case. "for pattern matching" from the usage message means "when used in pattern". And here a pattern is the second argument.

Knowing that we can easily explain your examples:


So, case 1,

MatchQ[HoldPattern[a[1]], HoldPattern[_[_]]]

is really trying to match _[_] to HoldPattern[a[1]], with success because it really is (HoldPattern)[(a[1])].

Furthermore, it will fail to match a[_] because this represents an expression which outer head is a while it should be HoldPattern.


Case 2 can be explained this way too. When the HoldPattern is stripped, _[_][_] doesn't match HoldPattern[a[1][1]] as it really is _[_[_][_]]:

MatchQ[HoldPattern[a[1][1]], HoldPattern[_[_[_][_]]]]
True

Possible issues with Verbatim:

To prevent HoldPattern from being stripped you can use Verbatim. But it can't be used mindlessly. E.g. let's say you have defined:

a[_]:=2

This will create

HoldPattern[a[_]] :> 2

down value. As disscusseddiscussed your approach won't work:

MatchQ[ HoldPattern[a[_]] :> 2, HoldPattern[a[_]] :> 2 ] (*False*)

but also

MatchQ[ HoldPattern[a[_]] :> 2, Verbatim[HoldPattern][a[_]] :> 2 ]

fails because there isn't anything preventing a[_] from evaluating to 2.

Here we are safe:

MatchQ[
 HoldPattern[a[_]] :> 2,
 Verbatim[HoldPattern][a[_]] :> 2 // HoldPattern
]

The answer finally:

DeleteCases[
 values,
 _[a[1][__]] :> _ // HoldPattern
]

Clarification of HoldPattern usage:

HoldPattern[expr] is equivalent to expr for pattern matching, but maintains expr in an unevaluated form.

I think this can be ambigious for begginers. One could think that what we are doing in

MatchQ[HoldPattern[a[1]], HoldPattern[_[_]]] 

is to more or less

MatchQ[ a[1], _[_] ] 

where both arguments are kept unevaluated. That's not the case. "for pattern matching" from the usage message means "when used in pattern". And here a pattern is the second argument.

Knowing that we can easily explain your examples:


So, case 1,

MatchQ[HoldPattern[a[1]], HoldPattern[_[_]]]

is really trying to match _[_] to HoldPattern[a[1]], with success because it really is (HoldPattern)[(a[1])].

Furthermore, it will fail to match a[_] because this represents an expression which outer head is a while it should be HoldPattern.


Case 2 can be explained this way too. When the HoldPattern is stripped, _[_][_] doesn't match HoldPattern[a[1][1]] as it really is _[_[_][_]]:

MatchQ[HoldPattern[a[1][1]], HoldPattern[_[_[_][_]]]]
True

Possible issues with Verbatim:

To prevent HoldPattern from being stripped you can use Verbatim. But it can't be used mindlessly. E.g. let's say you have defined:

a[_]:=2

This will create

HoldPattern[a[_]] :> 2

down value. As disscussed your approach won't work:

MatchQ[ HoldPattern[a[_]] :> 2, HoldPattern[a[_]] :> 2 ] (*False*)

but also

MatchQ[ HoldPattern[a[_]] :> 2, Verbatim[HoldPattern][a[_]] :> 2 ]

fails because there isn't anything preventing a[_] from evaluating to 2.

Here we are safe:

MatchQ[
 HoldPattern[a[_]] :> 2,
 Verbatim[HoldPattern][a[_]] :> 2 // HoldPattern
]

The answer finally:

DeleteCases[
 values,
 _[a[1][__]] :> _ // HoldPattern
]

Clarification of HoldPattern usage:

HoldPattern[expr] is equivalent to expr for pattern matching, but maintains expr in an unevaluated form.

I think this can be ambiguous for beginners. One could think that what we are doing in

MatchQ[HoldPattern[a[1]], HoldPattern[_[_]]] 

is to more or less

MatchQ[ a[1], _[_] ] 

where both arguments are kept unevaluated. That's not the case. "for pattern matching" from the usage message means "when used in pattern". And here a pattern is the second argument.

Knowing that we can easily explain your examples:


So, case 1,

MatchQ[HoldPattern[a[1]], HoldPattern[_[_]]]

is really trying to match _[_] to HoldPattern[a[1]], with success because it really is (HoldPattern)[(a[1])].

Furthermore, it will fail to match a[_] because this represents an expression which outer head is a while it should be HoldPattern.


Case 2 can be explained this way too. When the HoldPattern is stripped, _[_][_] doesn't match HoldPattern[a[1][1]] as it really is _[_[_][_]]:

MatchQ[HoldPattern[a[1][1]], HoldPattern[_[_[_][_]]]]
True

Possible issues with Verbatim:

To prevent HoldPattern from being stripped you can use Verbatim. But it can't be used mindlessly. E.g. let's say you have defined:

a[_]:=2

This will create

HoldPattern[a[_]] :> 2

down value. As discussed your approach won't work:

MatchQ[ HoldPattern[a[_]] :> 2, HoldPattern[a[_]] :> 2 ] (*False*)

but also

MatchQ[ HoldPattern[a[_]] :> 2, Verbatim[HoldPattern][a[_]] :> 2 ]

fails because there isn't anything preventing a[_] from evaluating to 2.

Here we are safe:

MatchQ[
 HoldPattern[a[_]] :> 2,
 Verbatim[HoldPattern][a[_]] :> 2 // HoldPattern
]

The answer finally:

DeleteCases[
 values,
 _[a[1][__]] :> _ // HoldPattern
]
added 2 characters in body
Source Link
Kuba
  • 137.7k
  • 13
  • 289
  • 751

Clarification of HoldPattern usage:

HoldPattern[expr] is equivalent to expr for pattern matching, but maintains expr in an unevaluated form.

I think this can be ambigious for begginers. One could think that what we are doing in

MatchQ[HoldPattern[a[1]], HoldPattern[_[_]]] 

is to more or less

MatchQ[ a[1], _[_] ] 

where both arguments are kept unevaluated. That's not the case. "for pattern matching" from the usage message means "when used in pattern". And here a pattern is the second argument.

Knowing that we can easily explain your examples:


So, case 1,

MatchQ[HoldPattern[a[1]], HoldPattern[_[_]]]

is really trying to match _[_] to HoldPattern[a[1]], with success because it really is (HoldPattern)[(a[1])].

Furthermore, it will fail to match a[_] because this represents an expression which outer head is the a while it should be HoldPattern.


Case 2 can be explained this way too. When the HoldPattern is stripped, _[_][_] doesn't match HoldPattern[a[1][1]] as it really is _[_[_][_]]:

MatchQ[HoldPattern[a[1][1]], HoldPattern[_[_[_][_]]]]
True

Possible issues with Verbatim:

To prevent HoldPattern from being stripped you can use Verbatim. But it can't be used mindlessly. E.g. let's say you have defined:

a[_]:=2

This will create

HoldPattern[a[_]] :> 2

down value. As disscussed your approach won't work:

MatchQ[ HoldPattern[a[_]] :> 2, HoldPattern[a[_]] :> 2 ] (*False*)

but also

MatchQ[ HoldPattern[a[_]] :> 2, Verbatim[HoldPattern][a[_]] :> 2 ]

fails because there isn't anything preventing a[_] to evalautefrom evaluating to 2.

Here we are safe:

MatchQ[
 HoldPattern[a[_]] :> 2,
 Verbatim[HoldPattern][a[_]] :> 2 // HoldPattern
]

The answer finally:

DeleteCases[
 values,
 _[a[1][__]] :> _ // HoldPattern
]

Clarification of HoldPattern usage:

HoldPattern[expr] is equivalent to expr for pattern matching, but maintains expr in an unevaluated form.

I think this can be ambigious for begginers. One could think that what we are doing in

MatchQ[HoldPattern[a[1]], HoldPattern[_[_]]] 

is to more or less

MatchQ[ a[1], _[_] ] 

where both arguments are kept unevaluated. That's not the case. "for pattern matching" from the usage message means "when used in pattern". And here pattern is the second argument.

Knowing that we can easily explain your examples:


So, case 1,

MatchQ[HoldPattern[a[1]], HoldPattern[_[_]]]

is really trying to match _[_] to HoldPattern[a[1]], with success because it really is (HoldPattern)[(a[1])].

Furthermore, it will fail to match a[_] because this represents an expression which outer head is the a while it should be HoldPattern.


Case 2 can be explained this way too. When the HoldPattern is stripped, _[_][_] doesn't match HoldPattern[a[1][1]] as it really is _[_[_][_]]:

MatchQ[HoldPattern[a[1][1]], HoldPattern[_[_[_][_]]]]
True

Possible issues with Verbatim:

To prevent HoldPattern from being stripped you can use Verbatim. But it can't be used mindlessly. E.g. let's say you have defined:

a[_]:=2

This will create

HoldPattern[a[_]] :> 2

down value. As disscussed your approach won't work:

MatchQ[ HoldPattern[a[_]] :> 2, HoldPattern[a[_]] :> 2 ] (*False*)

but also

MatchQ[ HoldPattern[a[_]] :> 2, Verbatim[HoldPattern][a[_]] :> 2 ]

fails because there isn't anything preventing a[_] to evalaute to 2.

Here we are safe:

MatchQ[
 HoldPattern[a[_]] :> 2,
 Verbatim[HoldPattern][a[_]] :> 2 // HoldPattern
]

The answer finally:

DeleteCases[
 values,
 _[a[1][__]] :> _ // HoldPattern
]

Clarification of HoldPattern usage:

HoldPattern[expr] is equivalent to expr for pattern matching, but maintains expr in an unevaluated form.

I think this can be ambigious for begginers. One could think that what we are doing in

MatchQ[HoldPattern[a[1]], HoldPattern[_[_]]] 

is to more or less

MatchQ[ a[1], _[_] ] 

where both arguments are kept unevaluated. That's not the case. "for pattern matching" from the usage message means "when used in pattern". And here a pattern is the second argument.

Knowing that we can easily explain your examples:


So, case 1,

MatchQ[HoldPattern[a[1]], HoldPattern[_[_]]]

is really trying to match _[_] to HoldPattern[a[1]], with success because it really is (HoldPattern)[(a[1])].

Furthermore, it will fail to match a[_] because this represents an expression which outer head is a while it should be HoldPattern.


Case 2 can be explained this way too. When the HoldPattern is stripped, _[_][_] doesn't match HoldPattern[a[1][1]] as it really is _[_[_][_]]:

MatchQ[HoldPattern[a[1][1]], HoldPattern[_[_[_][_]]]]
True

Possible issues with Verbatim:

To prevent HoldPattern from being stripped you can use Verbatim. But it can't be used mindlessly. E.g. let's say you have defined:

a[_]:=2

This will create

HoldPattern[a[_]] :> 2

down value. As disscussed your approach won't work:

MatchQ[ HoldPattern[a[_]] :> 2, HoldPattern[a[_]] :> 2 ] (*False*)

but also

MatchQ[ HoldPattern[a[_]] :> 2, Verbatim[HoldPattern][a[_]] :> 2 ]

fails because there isn't anything preventing a[_] from evaluating to 2.

Here we are safe:

MatchQ[
 HoldPattern[a[_]] :> 2,
 Verbatim[HoldPattern][a[_]] :> 2 // HoldPattern
]

The answer finally:

DeleteCases[
 values,
 _[a[1][__]] :> _ // HoldPattern
]
added 122 characters in body
Source Link
Kuba
  • 137.7k
  • 13
  • 289
  • 751

Clarification of HoldPattern usage:

HoldPattern[expr] is equivalent to expr for pattern matching, but maintains expr in an unevaluated form.

I think this can be ambigious for begginers. One could think that what we are doing in

MatchQ[HoldPattern[a[1]], HoldPattern[_[_]]] 

is to more or less

MatchQ[ a[1], _[_] ] 

where both arguments are kept unevaluated. That's not the case. "for pattern matching" from the usage message means "when used in pattern". And here pattern is the second argument.

Knowing that we can easily explain your examples:


So, case 1,

MatchQ[HoldPattern[a[1]], HoldPattern[_[_]]]

is really trying to match _[_] to HoldPattern[a[1]], with success because it really is (HoldPattern)[(a[1])].

Furthermore, it will fail to match a[_] because this represents an expression which outer head is the a while it should be HoldPattern.


Case 2 can be explained this way too. When the HoldPattern is stripped, _[_][_] doesn't match HoldPattern[a[1][1]] as it really is _[_[_][_]]:

MatchQ[HoldPattern[a[1][1]], HoldPattern[_[_[_][_]]]]
True

Possible issues with Verbatim:

To prevent HoldPattern from being stripped you can use Verbatim. But it can't be used mindlessly. E.g. let's say you have defined:

a[_]:=2

This will create

HoldPattern[a[_]] :> 2

down value. As disscussed your approach won't work:

MatchQ[ HoldPattern[a[_]] :> 2, HoldPattern[a[_]] :> 2 ] (*False*)

but also

MatchQ[ HoldPattern[a[_]] :> 2, Verbatim[HoldPattern][a[_]] :> 2 ]

fails because there isn't anything preventing a[_] to evalaute to 2.

Here we are safe:

MatchQ[
 HoldPattern[a[_]] :> 2,
 Verbatim[HoldPattern][a[_]] :> 2 // HoldPattern
]

The answer finally:

DeleteCases[
 values,
 _[a[1][__]] :> _ // HoldPattern
]

Clarification of HoldPattern usage:

HoldPattern[expr] is equivalent to expr for pattern matching, but maintains expr in an unevaluated form.

I think this can be ambigious for begginers. One could think that what we are doing in

MatchQ[HoldPattern[a[1]], HoldPattern[_[_]]] 

is to more or less

MatchQ[ a[1], _[_] ] 

where both arguments are kept unevaluated. That's not the case. "for pattern matching" from the usage message means "when used in pattern". And here pattern is the second argument.

Knowing that we can easily explain your examples:


So, case 1,

MatchQ[HoldPattern[a[1]], HoldPattern[_[_]]]

is really trying to match _[_] to HoldPattern[a[1]], with success because it really is (HoldPattern)[(a[1])].

Furthermore, it will fail to match a[_] because this represents an expression which outer head is the a while it should be HoldPattern.


Case 2 can be explained this way too. When the HoldPattern is stripped, _[_][_] doesn't match HoldPattern[a[1][1]] as it really is _[_[_][_]]:

MatchQ[HoldPattern[a[1][1]], HoldPattern[_[_[_][_]]]]
True

Possible issues with Verbatim:

To prevent HoldPattern from being stripped you can use Verbatim. But it can't be used mindlessly. E.g. let's say you have defined:

a[_]:=2

This will create

HoldPattern[a[_]] :> 2

down value. As disscussed your approach won't work:

MatchQ[ HoldPattern[a[_]] :> 2, HoldPattern[a[_]] :> 2 ] (*False*)

but also

MatchQ[ HoldPattern[a[_]] :> 2, Verbatim[HoldPattern][a[_]] :> 2 ]

fails because there isn't anything preventing a[_] to evalaute to 2.

Here we are safe:

MatchQ[
 HoldPattern[a[_]] :> 2,
 Verbatim[HoldPattern][a[_]] :> 2 // HoldPattern
]

Clarification of HoldPattern usage:

HoldPattern[expr] is equivalent to expr for pattern matching, but maintains expr in an unevaluated form.

I think this can be ambigious for begginers. One could think that what we are doing in

MatchQ[HoldPattern[a[1]], HoldPattern[_[_]]] 

is to more or less

MatchQ[ a[1], _[_] ] 

where both arguments are kept unevaluated. That's not the case. "for pattern matching" from the usage message means "when used in pattern". And here pattern is the second argument.

Knowing that we can easily explain your examples:


So, case 1,

MatchQ[HoldPattern[a[1]], HoldPattern[_[_]]]

is really trying to match _[_] to HoldPattern[a[1]], with success because it really is (HoldPattern)[(a[1])].

Furthermore, it will fail to match a[_] because this represents an expression which outer head is the a while it should be HoldPattern.


Case 2 can be explained this way too. When the HoldPattern is stripped, _[_][_] doesn't match HoldPattern[a[1][1]] as it really is _[_[_][_]]:

MatchQ[HoldPattern[a[1][1]], HoldPattern[_[_[_][_]]]]
True

Possible issues with Verbatim:

To prevent HoldPattern from being stripped you can use Verbatim. But it can't be used mindlessly. E.g. let's say you have defined:

a[_]:=2

This will create

HoldPattern[a[_]] :> 2

down value. As disscussed your approach won't work:

MatchQ[ HoldPattern[a[_]] :> 2, HoldPattern[a[_]] :> 2 ] (*False*)

but also

MatchQ[ HoldPattern[a[_]] :> 2, Verbatim[HoldPattern][a[_]] :> 2 ]

fails because there isn't anything preventing a[_] to evalaute to 2.

Here we are safe:

MatchQ[
 HoldPattern[a[_]] :> 2,
 Verbatim[HoldPattern][a[_]] :> 2 // HoldPattern
]

The answer finally:

DeleteCases[
 values,
 _[a[1][__]] :> _ // HoldPattern
]
added 650 characters in body
Source Link
Kuba
  • 137.7k
  • 13
  • 289
  • 751
Loading
added 650 characters in body
Source Link
Kuba
  • 137.7k
  • 13
  • 289
  • 751
Loading
Source Link
Kuba
  • 137.7k
  • 13
  • 289
  • 751
Loading