Re: Transformation rules - explain please
- To: mathgroup at smc.vnet.net
- Subject: [mg74073] Re: Transformation rules - explain please
- From: "Jean-Marc Gulliet" <jeanmarc.gulliet at gmail.com>
- Date: Thu, 8 Mar 2007 04:42:20 -0500 (EST)
- References: <esjg77$279$1@smc.vnet.net> <45ED7285.2050908@gmail.com>
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. > >