Re: Bug in pattern matching?
- To: mathgroup at smc.vnet.net
- Subject: [mg8754] Re: Bug in pattern matching?
- From: Rolf Mertig <Rolf at mertig.com>
- Date: Sat, 20 Sep 1997 22:28:19 -0400
- Organization: XS4ALL, networking for the masses
- Sender: owner-wri-mathgroup at wolfram.com
Peter Str=F6mbeck wrote:
>=20
> Hi, I've posted this problem to Wolfram support but received no answer =
so I
> thought I might just post it here aswell.
>=20
> I'm trying to "deconstruct" a list, perform an operation to a pert of t=
he
> list and then recombine the result. When I did this I found out that
> multiplication in pattern matching performs differently than otherwise.
>=20
> Check version:
>=20
> In[1]:=3D
> $Version
> Out[1]=3D
> "Microsoft Windows 3.0 (October 6, 1996)"
>=20
> Define list:
>=20
> In[2]:=3D
> ls=3DTable[a[j], {j,0,5}]
> Out[2]=3D
> {a[0],a[1],a[2],a[3],a[4],a[5]}
>=20
> I want to split this list into the following three sublists:
>=20
> {a[0],a[1],a[2],a[3],a[4],a[5]} -> {a[0]}, {a[1], a[2], a[3], a[4]}, {a=
[5]}
>=20
> Use pattern matching:
>=20
> In[3]:=3D
> ls/.{first_, middle___, last_}\[Rule] {{first},{middle},{last}}
> Out[3]=3D
> {{a[0]},{a[1],a[2],a[3],a[4]},{a[5]}}
>=20
> This works fine. Now I want to multiply the middle list by a factor C.
> Ordinary multiplication works like this:
>=20
> In[4]:=3D
> C ls
> Out[4]=3D
> {C a[0],C a[1],C a[2],C a[3],C a[4],C a[5]}
>=20
> Doing this in a pattern matching statement produces a totally different
> result:
>=20
> In[5]:=3D
> ls/.{first_, middle___, last_}\[Rule] {{first},C {middle},{last}}
> Out[5]=3D
> {{a[0]},{C a[1] a[2] a[3] a[4]},{a[5]}}
>=20
> The middle list now contains a completely different result than Out[4]!
>=20
> Can anyone please explain this to me?
>=20
> /Peter Str=F6mbeck
This is not a bug.
In order to get what you want, just use :> instead of ->=20
The "culprit" are the different Attributes of :> and ->=20
as well as the properties of Sequence.
Checking out the operations Mathematica does by using Trace
clarifies the issue:
In[16]:=3D ls/.{first_, middle___, last_}->
{{first},C {middle},{last}}//Trace
Out[16]=3D {{ls, {a[0], a[1], a[2], a[3], a[4], a[5]}},
> {{{C {middle}, {C middle}}, {{first}, {C middle}, {last}}},
> {first_, middle___, last_} -> {{first}, {C middle}, {last}},
> {first_, middle___, last_} -> {{first}, {C middle}, {last}}},
> {a[0], a[1], a[2], a[3], a[4], a[5]} /.
> {first_, middle___, last_} -> {{first}, {C middle}, {last}},
> {{a[0]}, {C a[1] a[2] a[3] a[4]}, {a[5]}}}
In[17]:=3D ls/.{first_, middle___, last_}:>=20
{{first},C {middle},{last}}//Trace
Out[17]=3D {{ls, {a[0], a[1], a[2], a[3], a[4], a[5]}},
> {a[0], a[1], a[2], a[3], a[4], a[5]} /.
> {first_, middle___, last_} :> {{first}, C {middle}, {last}},
> {{a[0]}, C {a[1], a[2], a[3], a[4]}, {a[5]}},
> {C {a[1], a[2], a[3], a[4]}, {C a[1], C a[2], C a[3], C a[4]}},
> {{a[0]}, {C a[1], C a[2], C a[3], C a[4]}, {a[5]}}}
In[18]:=3D ?Sequence
Sequence[expr1, expr2, ... ] represents a sequence of arguments to be
spliced
automatically into any function.
In[19]:=3D C Sequence[a[1],a[2],a[3],a[4],a[5]]
Out[19]=3D C a[1] a[2] a[3] a[4] a[5]
Rolf Mertig
http://www.mertig.com