Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Pattern Matching with ReplaceRepated Behaving Unexpectedly
Replies: 0

 Brentt Posts: 60 Registered: 9/24/11
Pattern Matching with ReplaceRepated Behaving Unexpectedly
Posted: Dec 4, 2012 4:09 AM

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.