MathGroup Archive 2005

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

Search the Archive

Re: A question about pattern

see 'The Book' 2.3.5 Patterns::Putting Constraints on Patterns:
In setting up patterns and transformation rules, there is often a choice of
where to put /; conditions. For example, you can put a /; condition on the
right-hand side of a rule in the form lhs :> rhs /; condition, or you can
put it on the left-hand side in the form lhs /; condition -> rhs.
( !!! ) You may also be able to insert the condition inside the expression
lhs. The only constraint is that all the names of patterns that you use in a
particular condition must appear in the pattern to which the condition is
( !!! ) If this is not the case, then some of the names needed to evaluate
the condition may not yet have been "bound" in the pattern-matching process.
If this happens, then Mathematica uses the global values for the
corresponding variables, rather than the values determined by pattern
so: {1, 2, 3, 4, 5} /. {a___, b_, c_, d___} /; (c > b) -> {a, f@ b, g@ c, d}
gives {f[1], g[2], 3, 4, 5}
and a test on c_  can only look 'inside' pattern c_, while a test or
condition on the entire lhs does what you want.

also check:
ReplaceList[{1,2,3,4,5,6}, {a___, b_, c_, d___} /; (c > b) -> {a,f@ b,g@
giving all possible results, whence ReplaceAll only returns the first:
{{f[1], g[2], 3, 4, 5, 6},
{1, f[2], g[3], 4, 5, 6},
{1, 2, f[3], g[4], 5, 6},
{1, 2, 3, f[4], g[5], 6},
{1, 2, 3, 4, f[5], g[6]}}

hint: read the manual, it's the best documentation I've ever seen.

"Daohua Song" <ds2081 at> wrote in message
news:ctcrok$6r2$1 at
> Dear Group,
>       I am just struggling by the following question: I want to build a
> rule like this,
>       List/.{a___,b_,c_?(#>b&),d___}->{a,2 b,3 c,d}
>       But it doesn't work.
>       please help me! Thanks

  • Prev by Date: Re: Re: random matrix from row and column sums
  • Previous by thread: Re: A question about pattern
  • Next by thread: NullSpace computation