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: [mg68727] Re: perplexed by blank sequence in pattern replacement
  • From: "Eckhard Hennig" <aidev at n-o-s-p-a-m.kaninkolo.de>
  • Date: Thu, 17 Aug 2006 04:18:34 -0400 (EDT)
  • References: <ebuj5d$6f5$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

"Blake" <laing at ou.edu> schrieb im Newsbeitrag 
news:ebuj5d$6f5$1 at smc.vnet.net...
> 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).

Hi Blake,

Mathematica evaluates the left-hand sides of transformation rules before 
they are applied. Therefore, the pattern Times[mysequence__] reduces to 
mysequence__ before it is matched against a*b*c.

To get the result you want, you must prevent Mathematica from evaluating the 
pattern. This can be done using the HoldPattern function as follows:

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

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

Best regards,

Eckhard

--
Dr.-Ing. Eckhard Hennig
www.kaninkolo.de/ai
aidev \at kaninkolo \dot de



  • Prev by Date: Re: Equal represented as HTML HEX entity number 63449 - is that correct?
  • 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