Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2012

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

Search the Archive

Pattern Matching with ReplaceRepated Behaving Unexpectedly

  • To: mathgroup at smc.vnet.net
  • Subject: [mg128928] Pattern Matching with ReplaceRepated Behaving Unexpectedly
  • From: Brentt <brenttnewman at gmail.com>
  • Date: Tue, 4 Dec 2012 04:13:50 -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

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.




  • Prev by Date: Creating a Piecewise function with imported list of arbitrary length
  • Next by Date: Could someone help optimize this for Mathematica 9?
  • Previous by thread: Re: Creating a Piecewise function with imported list of arbitrary length
  • Next by thread: Re: Pattern Matching with ReplaceRepated Behaving Unexpectedly