Re: Question about OneIdentity

*To*: mathgroup at smc.vnet.net*Subject*: [mg88306] Re: Question about OneIdentity*From*: m.r at inbox.ru*Date*: Thu, 1 May 2008 03:21:07 -0400 (EDT)*References*: <fv42gb$5r2$1@smc.vnet.net>

On Apr 28, 3:39 am, Szabolcs Horv=E1t <szhor... at gmail.com> wrote: > (Scroll down for the actual question) > > I never really understood Flat and OneIdentity, and unfortunately > documentation about them is scarce. > > From the docs: > > """ > OneIdentity is an attribute that can be assigned to a symbol f to > indicate that f[x], f[f[x]], etc. are all equivalent to x for the > purpose of pattern matching. > > OneIdentity has an effect only if f has attribute Flat. > """ > > ** Some comments: > > There is also an example, listing the Attributes of Times and showing > that Times[a] evaluates to a. However, this is misleading because this > behaviour cannot be caused by Times's attributes: > > In[1]:= Attributes[f] = Attributes[Times] > Out[1]= {Flat, Listable, NumericFunction, > OneIdentity, Orderless, Protected} > > In[2]:= f[a] > Out[2]= f[a] > > If we assign the same attributes to f, f[a] will not evaluate to a. Was= > the technical writer also confused, or is the example supposed to > illustrate something different than what I understood? > > ** And now the actual question: > > According to the text in the docs (f[x] is considered equivalent to x in > pattern matching) I would expect > > MatchQ[1, f[1]] > > to give True after evaluating SetAttributes[f, {Flat, OneIdentity}]. > But it gives False. > > ** The application: > > This came up in the following application: > > fun[HoldPattern@Plus[terms__]] := doSomething[{terms}] > > This function should handle a single term, too. Of course, there are > workarounds, but I couldn't come up with anything as simple as the > pattern above (which unfortunately does not work). This doesn't seem very logical to me, but it will work if you add a definition for Default: In[1]:= Attributes[f] = OneIdentity; Default[f] = 0; fun[f[terms_.]] := doSomething[List @@ terms] In[4]:= {fun[f[a, b]], fun[c]} Out[4]= {doSomething[{a, b}], doSomething[c]} Maxim Rytin m.r at inbox.ru