RE: perplexed by blank sequence in pattern replacement
- To: mathgroup at smc.vnet.net
- Subject: [mg68694] RE: [mg68674] perplexed by blank sequence in pattern replacement
- From: "David Park" <djmp at earthlink.net>
- Date: Thu, 17 Aug 2006 04:17:52 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Blake,
It is because the rule is evaluated on the lhs so it becomes:
Times[mysequence__] -> {mysequence}
mysequence__ -> {mysequence}
mysequence__ matches a*b*c so a*b*c is put into brackets to give {a*b*c}.
This is not an uncommon problem and the solution is to use HoldPattern.
Replace[a*b*c, HoldPattern[Times[mysequence__]] :> {mysequence}]
{a, b, c}
You could also use
a b c /. HoldPattern[Times[mysequence__]] -> {mysequence}
{a, b, c}
David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/
From: Blake [mailto:laing at ou.edu]
To: mathgroup 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).
Blake Laing
thesis slave
University of Oklahoma