MathGroup Archive 2005

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

Search the Archive

Re: Unsorted Union

  • To: mathgroup at
  • Subject: [mg61475] Re: Unsorted Union
  • From: David Bailey <dave at>
  • Date: Wed, 19 Oct 2005 23:07:24 -0400 (EDT)
  • References: <dj4p6t$gpt$>
  • Sender: owner-wri-mathgroup at

Robert Schoefbeck wrote:
> Hello all,
> in the help section i found the following thing:
> UnsortedUnion[x_] := Module[{f}, f[y_] := (f[y] = Sequence[]; y); f/@x]
> It does just what it says:
> UnsortedUnion[{a,c,b,d,a,b}]={a,c,b,d}
> (ordinary Union would Sort the result)
> my question is: How does the definition work? I know all the syntax
> and i have been using these concepts for for some time now. But i cant 
> figure out what this definition does.
> Robert

That definition creates a temporary function f, which it maps across the 
input list ( /@ is the operator for Map). The first time f[c] (say) is 
called it adds a definition for f[c] and returns its argument c. 
However, the second time it is called an explicit definition for f[c] 
exists which returns Sequence[] - so the second c is replaced by 
Sequence[] - which eliminates the term altogether.

I'm fairly sure there would be more efficient ways to do this if the 
list were long or the function called frequently.

BTW, If you are new to Sequence, it is worth exploring it a bit - it can 
achieve some surprising things!

David Bailey

  • Prev by Date: Re: Re: Pure Function for String Selection
  • Next by Date: Re: Exporting XML as DocBook, etc
  • Previous by thread: Re: Unsorted Union
  • Next by thread: Re: Unsorted Union