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

Dear Blake,

In conection with my previous post I own to say that searching and
experiment is sometimes the best to achieve what you want.

Before proceeding, I strongly recomend the book of Gaylord et al.
(Introduction to
Programming with Mathematica 1996. There is also a more recent edition
regarding v. 5.0).

Consider the function definition f (adopted from the mentioned
reference):

In[3]:=
f[x_Plus]:=Apply[Times,x]

Then

In[7]:=
Trace[f[a+b+c]]
Out[7]=
{f[a + b + c], Times @@ (a + b + c), a b c}

Suppose now you want a rewrite rule to do the same thing.
A quick idea is

In[8]:=
g[x_]:=x/.Plus[z___]->Times[z]

However,

In[10]:=
Trace[g[a+b+c],TraceOriginal->True]
Out[10]=
{g[a + b + c], {g}, {a + b + c, {Plus}, {a}, {b}, {c}, a + b + c},
  g[a + b + c],
  a + b + c /.\[InvisibleSpace]+z___ -> Times[z], {ReplaceAll}, {a + b
+
      c}, {+z___ -> Times[z], {Rule}, {+z___, {Plus}, {z___}, +z___,
      z___}, {Times[z], {Times}, {z}, Times[z], z}, z___ -> z,
    z___ -> z, {Rule}, {z___}, {z}, z___ -> z},
  a + b + c /.\[InvisibleSpace]z___ -> z, a + b + c}

As you can see the previous rule failed.
You must keep the LHS of the transformation rule unevaluated for
purposes of pattern matching, and the RHS of the rule unevaluated until
the rule is used.

In the mentioned reference the function Literal is used as follows to
achieve the goal

In[11]:=
Clear[g]

In[12]:=
g[x_]:=x/.Literal[Plus[z___]]:>Times[z]

In[13]:=
g[a+b+c]//Trace
Out[13]=
{g[a + b + c],
  a + b + c /.\[InvisibleSpace]Literal[+z___] :> Times[z], +(a b c), a
b c}

However,

In[14]:=
Information[Literal]

>From In[14]:=
"Literal has been renamed HoldPattern."
Attributes[Literal]={HoldAll,Protected}


So, to achieve what you want, simply enter

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

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

or

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

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

You can see why these command succeed now, by use the Trace command

In[25]:=
Trace[Replace[a*b*c,HoldPattern[Times[mysequence__]]:>{mysequence}],TraceOriginal->True]

Out[25]=
{Replace[a b c,
    HoldPattern[
        Times[mysequence__]] :> {mysequence}], {Replace}, {a b c,
{Times}, \
{a}, {b}, {c},
    a b c}, {HoldPattern[
        Times[mysequence__]] :> {mysequence}, {RuleDelayed},
{HoldPattern[
        Times[mysequence__]], {HoldPattern},
      HoldPattern[Times[mysequence__]]},
    HoldPattern[Times[mysequence__]] :> {mysequence}},
  Replace[a b c, HoldPattern[Times[mysequence__]] :> {mysequence}], {a,
b,
    c}, {List}, {a}, {b}, {c}, {a, b, c}}



Best regards,

Jim Anagnostou




Î?/Î? 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: Memory leak - Table & Export
  • Next by Date: Re: perplexed by blank sequence in pattern replacement
  • Previous by thread: Re: perplexed by blank sequence in pattern replacement
  • Next by thread: Re: perplexed by blank sequence in pattern replacement