Pattern
name{BLANKFORM}
is equivalent to Pattern[name, {BLANKFORM}]
and can be used in pattern matching to refer to the matched expression as name
, where {BLANKFORM}
is one of {_, __, ___}
.
name{BLANKFORM}head
is equivalent to Pattern[name, {BLANKFORM}head]
and can be used in pattern matching to refer to the matched expression as name
, where {BLANKFORM}
is one of {_, __, ___}
.
Attributes[Pattern] := {HoldFirst, Protected}
Simple examples
To demonstrate referencing name
in the replacement RHS:
In[1]:= (foo[2, 1]) /. ((foo[a_, b_] -> a)) Out[1]= 2
If two matches share the same name, they must be equivalent:
In[2]:= (foo[2, 1]) /. ((foo[a_, a_] -> a)) Out[2]= foo[2, 1]
In[3]:= (foo[2, 2]) /. ((foo[a_, a_] -> a)) Out[3]= 2
To demonstrate the head matching capability:
In[4]:= MatchQ[2, a_Integer] Out[4]= True
In[5]:= MatchQ[2, a_Real] Out[5]= False
Further examples
To demonstrate patterns matching a sequence of expressions:
In[1]:= (foo[2, 1]) /. ((foo[a___Integer] -> bar[a])) Out[1]= bar[2, 1]