Re: rules and lists

• To: mathgroup at smc.vnet.net
• Subject: [mg54246] Re: rules and lists
• From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
• Date: Mon, 14 Feb 2005 21:50:38 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```>-----Original Message-----
>From: Erich Neuwirth [mailto:erich.neuwirth at univie.ac.at]
To: mathgroup at smc.vnet.net
>Sent: Monday, February 14, 2005 4:17 AM
>To: mathgroup at smc.vnet.net
>Subject: [mg54246]  rules and lists
>
>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?
>
>

Eric,

In[1]:=
{1, 2, 3, 3, 1, 2} /. {x___, 1, 2, y___} -> {x, 1, 2, 3, y}
Out[1]=
{1, 2, 3, 3, 3, 1, 2}

doesn't do, because ReplaceAll just stops at the first match and
replacement.

ReplaceRepeated doesn't help in your case, because the replacement
contains the search pattern, and thus will loop indefinitely:

In[2]:=
{1, 2, 3, 3, 1, 2} //. {x___, 1, 2, y___} -> {x, 1, 2, 3, y}
Out[2]=
\$Aborted

To avoid that, make your replacements in two steps, first replace the
searched sequence 1,2 by a symbol

In[3]:=
{1, 2, 3, 3, 1, 2} //. {x___, 1, 2, y___} -> {x, \$, y}
Out[3]=
{\$, 3, 3, \$}

and then replace that symbol by the substitution sequence 1,2,3:

In[5]:=
{1, 2, 3, 3, 1, 2} //. {x___, 1, 2, y___} -> {x, \$, y} //.
{x___, \$, y___} -> {x, 1, 2, 3, y}
Out[5]=
{1, 2, 3, 3, 3, 1, 2, 3}

--
Hartmut Wolf

```

• Prev by Date: Simplifying an expression in light of relationships between variables?
• Next by Date: Re: UpValues for Blank[foo]
• Previous by thread: Re: rules and lists
• Next by thread: finding out what Automatic was