Re: Flat, OneIdentity attributes
- To: mathgroup at smc.vnet.net
- Subject: [mg21632] Re: Flat, OneIdentity attributes
- From: "Drago Ganic" <drago.ganic at in2.hr>
- Date: Tue, 18 Jan 2000 02:35:18 -0500 (EST)
- References: <85u3e7$d5j@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi Ted !
The Mathematica Book section 2.3.7 says
"However, in the case where x_ matches a single argument in a flat function, the question comes up as to whether the object it matches is really just the argument a itself, or f[a]. Mathematica chooses the first of these cases if the function carries the attribute OneIdentity, and chooses the second case otherwise. " <<=========
In our case f[2] is transformed to f [ f[2] ]
When you use
In -> f[2] /. f[n_] :> n+10
Out -> 10+ f[2]
you can see it clear.
For the input
f[2] /. f[n_Integer] :> n+10
f[2]
we don't have a match, because the _head_ of the argument is f and not Integer (the argument is f [2]).
Try
f[2] /. f[n_f] :> n+10
10 + f[2]
Exactly this problem is eliminated with OneIdentity.
Hope, I helped.
Greetings from Croatia,
Drago Ganic
"Ersek, Ted R" <ErsekTR at navair.navy.mil> wrote in message news:85u3e7$d5j at smc.vnet.net...
> 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
>