MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

Flat, OneIdentity attributes


For the most part I understand how Flat and OneIdentity are related and I
demonstrate this using Version 4 in the examples below.

In the first example (f) has the attributes Flat and OneIdentity. 
The pattern matcher treats f[a,2,3] as f[a,f[2,3]] then uses the 
replacement rule and {1,{2,3}} is returned.

In[1]:=
ClearAll[f];
Attributes[f]={Flat,OneIdentity};
f[1,2,3]//.f[a_,b_]:>{a,b}

Out[3]=
{1,{2,3}}

---------------------------------------------------
In the next example the only attribute (f) has is Flat.
In this case the pattern matcher treats f[1,2,3] as 
f[f[1],f[f[2],f[3]]] then uses the replacement rule and 
{f[1],{f[2],f[3]}} is returned.


In[4]:=
ClearAll[f];
Attributes[f]={Flat};
f[1,2,3]//.f[a_,b_]:>{a,b}

Out[6]=
{f[1],{f[2],f[3]}}

OneIdentity the pattern matcher doesn't wrap (f) around a single argument
when it tries different ways of nesting (f).

--------------------------------
In the next example (f) has the attributes Flat, OneIdentity and the rule is
used.

In[7]:=
ClearAll[f]
Attributes[f]={Flat,OneIdentity};
f[2]/.f[n_Integer]:>n+10

Out[9]=
12

--------------------------------
For reasons I can't understand the rule isn't used in the next example. Can
anyone explain why?

In[10]:=
ClearAll[f]
Attributes[f]={Flat};
f[2]/.f[n_Integer]:>n+10

Out[12]=
f[2]

---------------------------------------------
Regards,
Ted Ersek

For Mathematica tips, tricks see 
http://www.dot.net.au/~elisha/ersek/Tricks.html


  • Prev by Date: Re: generating subsets
  • Next by Date: Re: step fn of sin
  • Previous by thread: Re: Mathematica???
  • Next by thread: Re: Flat, OneIdentity attributes