Re: Function with attributes Flat and OneIdentity
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg1098] Re: Function with attributes Flat and OneIdentity
- From: beretta at ATHENA.MIT.EDU (Robert K Beretta)
- Date: Sun, 14 May 1995 22:21:35 -0400
- Organization: Massachusetts Institute of Technology
In article <3oru78$psl at news0.cybernetics.net> Marc Mazzariol <mmazzari at didec14.epfl.ch> writes in part: >So my problem is that i want to create a function like Plus : > > Plus has the attributes Flat and OneIdentity > Plus[2] gives 2 (and not Plus[2] like my function r does) > Plus[Plus[2]] gives 2 ( "" "" "" ) > Plus[2,2] gives 4 (and not $Itera... like my function r does) > >How can I do it ???? In answer to a related question of my own, Dave Withoff at WRI gave me an example that showed that the relative order of attribute and downvalue assignments can be relevant to the behavior of a function. If assignments are made as follows, I believe you get the behavior you want: In[1]:= ClearAll[r] r[x_]:=x Attributes[r] = {Flat}; In[2]:= r[2] Out[2]= 2 In[3]:= r[r[2]] Out[3]= 3 In[4]:= r[2, 2] Out[4]= r[2, 2] In[5]:= r[2, r[3, 2]] Out[5]= r[2, 3, 2] However, if the attribute and downvalue assignments are made in the opposite order, the behavior will change: In[6]:= ClearAll[r] Attributes[r] = {Flat}; r[x_]:=x In[7]:= r[2] $IterationLimit::itlim: Iteration limit of 4096 exceeded. Out[7]= Hold[r[2]] I'm afraid I can't offer a low-level explanation for this behavior, maybe someone else can clarify. Bob B.