Re: Transformation rules - explain please
- To: mathgroup at smc.vnet.net
- Subject: [mg74140] Re: [mg74023] Transformation rules - explain please
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Mon, 12 Mar 2007 22:05:57 -0500 (EST)
- References: <200703061032.FAA02186@smc.vnet.net>
On 6 Mar 2007, at 11:32, wooks wrote:
> This is supposed to substitute the first occurrence of old1 or old2
> with new.
>
> Why does this work
>
> Clear[old1, old, old2, lat, MySubst]
> lat = {banana, ice, cream, smeared, chocolate, topping};
> MySubst[new_, old1_, old2_, {x___, old_, r___}] := {x, new, r} /;
> Or[old == old1, old == old2];
> MySubst[strawberry, chocolate, cream, lat]
>
> but not this (with the conditional on the lhs).
>
> Clear[old1, old, old2, lat, MySubst]
> lat = {banana, ice, cream, smeared, chocolate, topping};
> MySubst[new_, old1_, old2_, {x___, old_, r___} /; Or[old = old1, old
> = old2]] := {x, new, r};
> MySubst[strawberry, chocolate, cream, lat]
>
> I like the idea of using conditional rules like this but have found it
> very hit and miss. Often times things don't work so I am seeking
> further enlightment.
>
> I'd be interested to see what would need to be done to make the
> conditional work on the lhs.
>
>
Your definition with Condition on the RHS is equivalent to the
following defintion with Condition on the LHS, which also works:
MySubst[new_, old1_, old2_, {x___, old_, r___} ] /; Or[old == old1, old
== old2] := {x, new, r};
The problem with your attempt
> MySubst[new_, old1_, old2_, {x___, old_, r___} /; Or[old = old1, old
> = old2]] := {x, new, r};
is not that Condition is on the left, but that it is being used to
test a pattern
{x___, old_, r___} /; old == old1 =E2=88=A8 old == old2, which can never
match until old1 or old2 has been assigned a value. To see that
better note that the following (rather pointless) defintion actually
"works":
Clear[old1, old, old2, lat, MySubst]
lat = {banana, ice, cream, smeared, chocolate, topping};
MySubst[new_, old1_, old2_, {x___, old_, r___} /; Or[old ==
chocolate, old
== old2]] := {x, new, r}
MySubst[strawberry, chocolate, cream, lat]
{banana,ice,cream,smeared,strawberry,topping}
This time the pattern {x___, old_, r___} /; Or[old == chockolate,
old
== old2] matched even before old2 had any value and the rule was
applied.
Andrzej Kozlowski
- References:
- Transformation rules - explain please
- From: "wooks" <wookiz@hotmail.com>
- Transformation rules - explain please