Flat, OneIdentity attributes

• To: mathgroup at smc.vnet.net
• Subject: [mg21600] Flat, OneIdentity attributes
• From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
• Date: Sun, 16 Jan 2000 22:43:47 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```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