MathGroup Archive 2009

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

Search the Archive

Re: Re: OneIdentity


On 25 Sep 2009, at 18:57, Chris Osborn wrote:

> On Sep 24, 6:51 am, Andrzej Kozlowski <a... at mimuw.edu.pl> wrote:
>> 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
>
> Thanks Andrzej,
>
> Your post is helpful.  I find the following behavior amusing:
>
> In[1438]:= MatchQ[monkeyfish[turtle], Plus[_ : car]]
>
> Out[1438]= True
>
>
> Where none of my cute symbols have any defs associated with them.
>
> I indeed searched for OneIdentity before I posted my questions, but
> for some reason 0 results were returned.
> Now when I do a search, I get this one thread back.
>
> Thanks,
> Chris
>


How about this:

http://forums.wolfram.com/mathgroup/archive/2000/Jan/msg00202.html

this

http://forums.wolfram.com/mathgroup/archive/2000/Jan/msg00254.html

this

http://forums.wolfram.com/mathgroup/archive/2000/Jan/msg00207.html


and quite a few others in these and other threads ?


Andrzej Kozlowski


  • Prev by Date: Monitoring status of dynamic calculation
  • Next by Date: How to solve this equation?
  • Previous by thread: Re: OneIdentity
  • Next by thread: Re: OneIdentity