MathGroup Archive 2007

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

Search the Archive

Re: rule based program for "Deleting repeated members of a list."

  • To: mathgroup at smc.vnet.net
  • Subject: [mg82034] Re: rule based program for "Deleting repeated members of a list."
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Wed, 10 Oct 2007 04:23:27 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <fe7j44$qga$1@smc.vnet.net> <3917603.1191869840479.JavaMail.root@m35> <fefi83$jfi$1@smc.vnet.net>

DrMajorBob wrote:

> Union with one argument is the same thing as Union, and it sorts; the OP 
> didn't want it sorted.
> 
> Bobby
> 
> On Mon, 08 Oct 2007 01:07:55 -0500, sean_incali <sean_incali at yahoo.com> 
> wrote:
> 
>> How about a simplests way of using intersection, instead of rule
>> based.
>>
>> s = {a, b, c, c, d, e, e, f, g, g, g, h, a, b} // Intersection
>> {a, b, c, d, e, f, g, h}
>>
>>
>>
>>
>> On Oct 6, 2:05 am, mumat <csar... at gmail.com> wrote:
>>> Hi,
>>>
>>> I have a list s={a,b,c,c,d,e,e,f,g,g,g,h,a,b};
>>>
>>> I need to write a program so that it reads the list and ignors
>>> repeated elements; so it outputs the following:
>>>
>>> RepeatRemover[s]={a,b,c,d,e,f,g,h,a,b};
----------------------^^^-------------^^^
Note that not only the desired output is not sorted but also non 
consecutive possibly repeated elements must not be discarded.

>>>
>>> I am looking for a rule-based program to do this!
>>>
>>> Any help would be greatly appreciated.

To get the desired output, one must *map* Union, Intersection, or 
Complement onto the split list according to the running elements.

In[1]:= s = {a, b, c, c, d, e, e, f, g, g, g, h, a, b};
Union /@ Split@s // Flatten
Intersection /@ Split@s // Flatten
Complement /@ Split@s // Flatten
s //. {a___, b_, b_, c___} -> {a, b, c}
s //. {a___, b_, d_, e___} :> {a, b, e} /; b == d

Out[2]= {a, b, c, d, e, f, g, h, a, b}

Out[3]= {a, b, c, d, e, f, g, h, a, b}

Out[4]= {a, b, c, d, e, f, g, h, a, b}

Out[5]= {a, b, c, d, e, f, g, h, a, b}

Out[6]= {a, b, c, d, e, f, g, h, a, b}

Regards,
-- 
Jean-Marc


  • Prev by Date: Re: NumberForm question
  • Next by Date: Add on for Dirac Notation and Quantum Computing in Mathematica 6
  • Previous by thread: Re: rule based program for "Deleting repeated members of a list."
  • Next by thread: Re: Re: rule based program for "Deleting repeated members of a list."