Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

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

Search the Archive

Re: Transformation rules - explain please

  • To: mathgroup at smc.vnet.net
  • Subject: [mg74141] Re: Transformation rules - explain please
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Mon, 12 Mar 2007 22:06:29 -0500 (EST)
  • Organization: The Open University, Milton Keynes, UK
  • References: <esjg77$279$1@smc.vnet.net>

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]

You put your test to early. Before the function MySubst is called, the 
arguments are evaluated. As define above, the first three arguments can 
be any expressions; however, the last argument must be a list of one, 
two, or three elements *and* the argument called "old" *must* be equal 
to either old1 or old2. Only if all these conditions are meet the 
function is called. Since the list "lat" (as defined) does not meet 
these conditions (ice is not equal to chocolate or cream), MySubst is 
returned unevaluated (default Mathematica behavior).

What you need to investigate more is the standard evaluation sequence. 
See "The Mathematica Book / Part A: Mathematica Reference Guide / 
Section A.4: Evaluation"  at 
http://documents.wolfram.com/mathematica/book/section-A.4

HTH,
Jean-Marc

> 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: Matching two patterns?
  • Next by Date: Re: logical/set theoretic programming
  • Previous by thread: Re: Transformation rules - explain please
  • Next by thread: Simplifying {0,0,0}.X.{0,0,0}