Re: Pattern Matching with ReplaceRepated Behaving Unexpectedly
- To: mathgroup at smc.vnet.net
- Subject: [mg128942] Re: Pattern Matching with ReplaceRepated Behaving Unexpectedly
- From: Bob Hanlon <hanlonr357 at gmail.com>
- Date: Wed, 5 Dec 2012 03:11:18 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-newout@smc.vnet.net
- Delivered-to: mathgroup-newsend@smc.vnet.net
- References: <20121204091350.A8C1F68BD@smc.vnet.net>
The second rule is never seen since the first result matches the first rule. Note: {u, a, a, v} //. { {p___, x : Longest[(a_) ..], q___} :> (Print["Rule 1 applied"]; {p, a^Length[{x}], q}), {x___, a^2, y___} :> (Print["Rule 2 applied"]; {x, e, y})} Rule 1 applied Rule 1 applied {u, a^2, v} MatchQ[%, {p___, x : Longest[(a_) ..], q___}] True You can just reverse the order of the rules {u, a, a, v} //. { {x___, a^2, y___} :> (Print["Rule 1 applied"]; {x, e, y}), {p___, x : Longest[(a_) ..], q___} :> (Print["Rule 2 applied"]; {p, a^Length[{x}], q})} Rule 2 applied Rule 1 applied Rule 2 applied {u, e, v} Bob Hanlon On Tue, Dec 4, 2012 at 4:13 AM, Brentt <brenttnewman at gmail.com> wrote: > > I'm having trouble understanding how replace repeated works. > > I have this expression: > > In[1]:= {u,a, a,v} //. { > {p___, x : Longest[(a_) ..], q___} :> {p, a^Length[{x}], q}, > {x___, a^2, y___} :> {x, e, y} > } > > > What I think should happen here is that {u,a,a,v} should become {u,a^2,v} > and then, since ReplaceRepeated should make another pass, {u,a^2,v} should > become {u,e,v}. And actually if I break up the replacement rules, and > evaluate them one by one, this is precisely what happens. But when I try to > put the replacement rules into a list of rules with replace repeated as > above it just does the first rule and stops. I.e. it outputs > > Out[1]:= {u,a^2,v} > > Strangely if I do > > In[2]:= % //. { > {p___, x : Longest[(a_) ..], q___} :> {p, a^Length[{x}], q}, > {x___, a^2, y___} :> {x, e, y} > } > > I get the result I expected ReplaceRepeated to give me before > > Out[2]:= {u,e,v} > > > I actually thought Replace Repated exists for this very reason, to keep on > applying the rules until the rules do not change the output any longer, but > it is failing to do that in this case for some reason even though there > still seems to be patterns that should be changed? > > Can anyone tell me what I am missing here? > > Thank you, any help will be greatly appreciated. > >
- References:
- Pattern Matching with ReplaceRepated Behaving Unexpectedly
- From: Brentt <brenttnewman@gmail.com>
- Pattern Matching with ReplaceRepated Behaving Unexpectedly