I am trying to rewrite patterns. For example, I'd like to rewrite the pattern
jFoo_Integer
which has FullForm
Pattern[jFoo,Blank[Integer]]
into just
Blank[Integer]
In other words, I want to strip out the names of patterns. I tried the following
jFoo_Integer /. {Pattern[nym_, Blank[typ_]] :> {nym, typ}}
which does not match or reduce and produces the (IMO bogus) error message
Pattern::patvar: First element in pattern Pattern[nym_,Blank[typ_]] is not a valid pattern name. >>
I also tried
Pattern[jFoo,Blank[Integer]] /. {Pattern[nym_, Blank[typ_]] :> {nym, typ}}
Pattern[jFoo,Blank[Integer]] /. {Verbatim[Pattern][nym_, Blank[typ_]] :> {nym, typ}}
jFoo_Integer /. {Verbatim[Pattern][nym_, Blank[typ_]] :> {nym, typ}}
jFoo_Integer /. {Verbatim[Pattern][nym_, Blank[typ_]] :> {nym, typ}}
all with exactly the same (failed) results.
Any hints, please & thanks?
Answer
What you want is probably
jFoo_Integer /. Verbatim[Pattern][nym_, Verbatim[Blank][typ_]] :> {nym, typ}
(* {jFoo, Integer} *)
The usage of Verbatim
points it out
Verbatim[expr]
representsexpr
in pattern matching, requiring thatexpr
be matched exactly as it appears, with no substitutions for blanks or other transformations.
Comments
Post a Comment