|
Further Examples: Union
Here is the settheoretic union of a list of elements.
In[1]:= 
Out[1]= 
Here is another way to write the same expression.
In[2]:= 
Out[2]= 
This works since both ff[a, b, c] and ff[b, c, d] have the same head.
In[3]:= 
Out[3]= 
Union with one argument drops duplicates from a list and sorts the remaining elements.
In[4]:= 
Out[4]= 
We define the function UnsortedUnion that drops duplicates from a list but, unlike Union, does not sort the remaining elements. (See the Further Examples for Reap for another implementation.)
In[5]:= 
In[6]:= 
Out[6]= 
We define gg to explain how UnsortedUnion works.
In[7]:= 
Evaluate gg[5]. The compound expression (gg[5] = Sequence[ ]; 5) is evaluated, giving as the output.
In[8]:= 
Out[8]= 
Since the expression gg[5] = Sequence[ ] was evaluated the first time that gg[5] was called, subsequent evaluations of gg[5] give Sequence[ ].
In[9]:= 
Out[9]= 
Only the first occurrences of gg[a], gg[b], gg[c] and gg[d] appear; later occurrences do not, by the definition of Sequence[ ].
In[10]:= 
Out[10]= 
This is the same thing.
In[11]:= 
Out[11]= 
In[12]:= 
|