Pattern matching trivia
- To: mathgroup at smc.vnet.net
- Subject: [mg13628] Pattern matching trivia
- From: Ersek_Ted%PAX1A at mr.nawcad.navy.mil
- Date: Fri, 7 Aug 1998 03:08:29 -0400
- Sender: owner-wri-mathgroup at wolfram.com
I just discovered something interesting about pattern matching. The
definitions for (goo) are no applied in the lines below. I understand
this is
because an expression with the head Complex or Rational is an atom, and
is not
considered a sum or product of smaller expressions.
In[1]:=
goo[a_+b_]:={Complex,a,b}
goo[m_/n_]:={Rational,m,n}
In[3]:=
goo[2+3 I]
Out[3]=
goo[2+3 I]
In[4]:=
goo[2/3]//InputForm
Out[4]=
goo[2/3]
Now the function (foo) below has the attribute HoldAllComplete. The
peculiar thing is that the definitions are now used. It seems that
when the arguments of (foo) are Complex or Rational, the pattern
matcher now treats these arguments as the sum or product of smaller
expressions.
In[5]:=
Attributes[foo]={HoldAllComplete};
foo[a_+b_]:={Complex,a,b}
foo[m_/n_]:={Rational,m,n}
In[7]:=
foo[2+3I]
Out[7]=
{Complex,2,3 I}
In[8]:=
foo[2/3]
Out[8]=
{Rational,2,3}
It turns out Version 3.0 has exactly five built in functions with the
HoldAllComplete attribute. They are: HoldComplete
InterpretationBox
MakeBoxes
Parenthesize
Unevaluated
Note: Parenthesize is only documented via the usage message below. I
think that means it's an experimental feature.
In[9]:=
?Parenthesize
"Parenthesize[ expr, fmt, prec, group] will represent expr in format fmt
and \
parenthesize it if necessary."
__________________________________
Ted Ersek
PS
Starting 10 Aug 1998 my email address will be: ErsekTR at navair.navy.mil