MathGroup Archive 2005

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

Search the Archive

Re: rules and lists


Erich Neuwirth wrote:
> I would like to replace sublists of a list to be replaced by another 
> list, such that for example the rule
> {1,2}->{1,2,3}
> wout tranform
> {1,2,3,3,1,2} into
> {1,2,3,3,3,1,2,3}
> Simply gibbin the transformation rule in the form
> {x___,1,2,y___}->{x,1,2,3,y}
> does not work.
> What is the idiom to do what I want?
> 
This is slightly more subtle than it looks. The point is that /. will 
match your whole list with the pattern you have used - so it will only 
find one instance of your sublist. However, if you use //. you will get 
an infinite loop. The easiest way is to use a transformation that 
replaces each sublist with symbols - so you can use //. and match all 
the sublists, then replace the symbols with their proper values.

{1,2,3,3,1,2}//.{x___,1,2,y___}->{x,a1,a2,a3,y} /. {a1->1,a2->2,
     a3->3}

{1, 2, 3, 1, 2, 3, 2}

David Bailey
dbaileyconsultancy.co.uk


  • Prev by Date: Re: Using Select with arrays? (Relative newbie)
  • Next by Date: Simplifying an expression in light of relationships between variables?
  • Previous by thread: Re: rules and lists
  • Next by thread: Re: rules and lists