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: [mg68708] Re: perplexed by blank sequence in pattern replacement
  • From: albert <awnl at arcor.de>
  • Date: Thu, 17 Aug 2006 04:18:14 -0400 (EDT)
  • References: <ebuj5d$6f5$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi Blake,
 
> 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).

your understanding of pattern matching is alright, the matter here is that
the LHS of your rule is evaluated before the pattern matching even begins. 
Since Times[something] is evaluated to something the actual rule that
Replace sees is:

mysequence__:>{mysequence}

You can check this by either using Trace[] or just look at the fullform of:

In[4]:= Times[mysequence___]:>{mysequence}//FullForm

Out[4]//FullForm= RuleDelayed[Pattern[mysequence, BlankNullSequence[]],
List[mysequence]]

To avoid this, use HoldPattern on the LHS:

In[5]:= Replace[a*b*c,HoldPattern[Times[mysequence__]]:>{mysequence}]

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


hth,

albert


  • Prev by Date: Re: perplexed by blank sequence in pattern replacement
  • Next by Date: Re: namespace collision [bug]
  • Previous by thread: Re: perplexed by blank sequence in pattern replacement
  • Next by thread: RE: perplexed by blank sequence in pattern replacement