MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Transformation rules - explain please


Hi,

I assume you know the difference between
Set[] (=) and Equal[] (==) and you second version
uses Set[] instead of equal.

To match a pattern like {x___, old_, r___}
the pattern matcher must search the possible
matches. But you second version place the condition
on the pattern so it fails for the first time
before x___ and r___ are bounded.

Regards
   Jens


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.
> 
> 


  • Prev by Date: Re: Transformation rules - explain please
  • Next by Date: Integrate
  • Previous by thread: Re: Transformation rules - explain please
  • Next by thread: Re: Transformation rules - explain please