Re: OneIdentity
- To: mathgroup at smc.vnet.net
- Subject: [mg103488] Re: [mg103478] OneIdentity
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Thu, 24 Sep 2009 07:51:11 -0400 (EDT)
- References: <200909240352.XAA13458@smc.vnet.net>
The reason is that OneIdentity does not actually mean what the
documentation seems to imply. What it does exactly would take too long
to explain and it has been already done a number of times on this
forum . Here is a very brief and basic "explanation" with two examples
(if you search the archives for Flat, OneIdentity adding perhaps the
names Allan Hayes or Hartmut Wolf you will find a lot more on this
topic).
When used alone, without the Flat attribute, it is only significant in
connection with Default values. Here is an example.
Remove[plus];
MatchQ[1, plus[_ : 0]]
False
Remove[plus];
SetAttributes[plus, {OneIdentity}];
MatchQ[1, plus[_ : 0]]
True
When used together with the attribute Flat it has a rather different
significance:
Remove[f]
SetAttributes[f, Flat];
MatchQ[f["a", "b", "c", "d"], f[__, _String, __]]
False
but
Remove[f]
SetAttributes[f, {Flat, OneIdentity}];
MatchQ[f["a", "b", "c", "d"], f[__, _String, __]]
True
The reason why the first match did not work is that an additional f
was wrapped around the arguments. Having the OneIdentity attribute
prevents this form happening.
Andrzej Kozlowski
On 24 Sep 2009, at 12:52, Chris Osborn wrote:
> I do not understand why I get "False" instead of "True" here:
>
>
> In[773]:= ClearAll[key];
> SetAttributes[key, OneIdentity];
>
> MatchQ[13, key[_]]
>
> Out[775]= False
>
>
> According to 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.
>
>
> Thanks in advance,
> Chris
>
- References:
- OneIdentity
- From: Chris Osborn <chrisosb@gmail.com>
- OneIdentity