MathGroup Archive 2006

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

Search the Archive

Re: perplexed by blank sequence in pattern replacement

  • To: mathgroup at smc.vnet.net
  • Subject: [mg68721] Re: perplexed by blank sequence in pattern replacement
  • From: dimmechan at yahoo.com
  • Date: Thu, 17 Aug 2006 04:18:28 -0400 (EDT)
  • References: <ebuj5d$6f5$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Dear Blake,

I think you can see where is the problem by applying the Trace function
with the option
TracOriginal activated to True.

In[52]:=
Trace[Replace[a*b*c,Times[mysequence__]\[RuleDelayed]{mysequence}],\
TraceOriginal\[Rule]True]//InputForm

Out[53]=
{Replace[a b
c,Times[mysequence__]\[RuleDelayed]{mysequence}],{Replace},{a b
      c,{Times},{a},{b},{
      c},a b
c},{Times[mysequence__]\[RuleDelayed]{mysequence},{RuleDelayed},{\
Times[mysequence__],{Times},{
    mysequence__,{Pattern},{__},mysequence__},Times[mysequence__],
      mysequence__},

mysequence__\[RuleDelayed]{mysequence},mysequence__\[RuleDelayed]{\
mysequence},{RuleDelayed},{mysequence__},mysequence__\[RuleDelayed]{\
mysequence}},Replace[a b c,mysequence__\[RuleDelayed]{mysequence}],{a
      b c},{List},{a b c},{a b c}}

If I am not wrong, the problem is that pattern Times[mysequence__] is
evaluated to mysequence__  before application of the rule.

An idea to avoid this is the followg command:

In[59]:=
Replace[a*b*c,Times[x_,y___]:>{x,y}]

Out[59]=
{a,b,c}

You can see why this pattern matching gives what you want below.

In[60]:=
Trace[Replace[a*b*c,Times[x_,y___]:>{x,y}],TraceOriginal\[Rule]True]

Out[60]=
{Replace[a b c,x_
    y___\[RuleDelayed]{x,y}],{Replace},{a b c,{Times},{
      a},{b},{c},a b c},{x_ y___\[RuleDelayed]{x,y},{RuleDelayed},{x_ \
y___,{Times},{x_,{
    Pattern},{_},x_},{y___,{Pattern},{___},y___},x_ y___},x_
      y___\[RuleDelayed]{x,
    y},x_ y___\[RuleDelayed]{x,y},{RuleDelayed},{x_ y___},x_ y___\
\[RuleDelayed]{x,y}},Replace[a b c,x_ y___\[RuleDelayed]{
        x,y}],{a,b,c},{List},{a},{b},{c},{a,b,c}}


Cheers,
Jim

Î?/Î? Blake έγÏ?αÏ?ε:
> Dear MathGroup:
>
> I have been blithely using blank sequences in pattern matching for some
> time. In persuit of a bug in a package of mine, I was quite alarmed to
> find that I don't really understand how to use the blank sequence, as
> expressed in the following simplified example:
>
> In[1]:=Replace[a*b*c,Times[mysequence__]:>{mysequence}]
>
> Out[1]={a b c}
>
> I expected Out[1]={a,b,c}, from a naieve reading of the full form of
> a*b*c
>
> In[2]:=FullForm[a*b*c]
>
> Out[2]//FullForm=Times[a,b,c]
>
> Will someone PLEASE tell me why In[1] does not yield the results I
> expected? (I can readily use a work-around, what I am concerned with is
> a correct understanding of pattern matching).
> 
> Blake Laing
> thesis slave
> University of Oklahoma


  • Prev by Date: Re: namespace collision [bug]
  • Next by Date: Re: too many special linear matrices
  • Previous by thread: Re: perplexed by blank sequence in pattern replacement
  • Next by thread: Re: perplexed by blank sequence in pattern replacement