Re: Pattern matching trivia
- To: mathgroup at smc.vnet.net
- Subject: [mg13640] Re: Pattern matching trivia
- From: "Allan Hayes" <hay at haystack.demon.cc.uk>
- Date: Fri, 7 Aug 1998 09:41:09 +0100
- References: <6qcr0d$qln$5@dragonfly.wolfram.com>
- Sender: owner-wri-mathgroup at wolfram.com
Ersek_Ted%PAX1A at mr.nawcad.navy.mil wrote in message
<6qcr0d$qln$5 at dragonfly.wolfram.com>...
..........
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}
..........
Ted,
HoldFirst is sufficient:
ClearAll[foo];
Attributes[foo]={HoldFirst};
HoldPattern[foo[a_+b_]]:={Complex,a,b} foo[m_/n_]:={Rational,m,n}
{foo[2 + 3],foo[3/6]}
{{Complex,2,3},{Rational,3,6}}
This is related to
Hold[2+3I]//FullForm
Hold[Plus[2,Times[3,I]]]
2+3I//FullForm
Complex[2,3]
The conversion to Complex[..] form is made after evaluation.
Allan
------------------------------------------------------------- Allan
Hayes
Mathematica Training and Consulting
Leicester UK
http://www.haystack.demon.co.uk
hay at haystack.demon.co.uk
voice: +44 (0)116 271 4198
fax: +44(0)116 271 8642