Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

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

Blake,

The key point to recognize is that Times (like Plus) applies built-in
rules before any user defined rules. Also Times[a] =>a
Now suppose we had a user defined function f that we wanted to create a
list of its arguments then

In[86]:=
f[a,b,c]/.f[x__]\[RuleDelayed]List[x]

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

But if we use Times instead of f then we must prevent its arguments
from being evaluated. One way is to use Hold

In[94]:=
Times[Hold[a,b,c]]/.Hold[x__]\[RuleDelayed]List[x]

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

Another way is

In[97]:=
Times[Hold[a,b,c]]/.Times[x__]\[RuleDelayed]List[x]//ReleaseHold

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

In this case the LHS of the rule Times[x__] evaluates to x__ which
matches Hold[a,b,c], and thus we need to apply ReleaseHold. Remember
Times[Hold[a,b,c]] evaluates to Hold[a,b,c]

Hope this helps,
Cheers

Brian


Blake wrote:
> 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: too many special linear matrices
  • 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