Re: A question about pattern
- To: mathgroup at smc.vnet.net
- Subject: [mg53850] Re: A question about pattern
- From: "wouter meeussen" <wouter.meeussen at pandora.be>
- Date: Sun, 30 Jan 2005 03:18:11 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
see 'The Book' 2.3.5 Patterns::Putting Constraints on Patterns: quote 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 attached. ( !!! ) 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 matching. end.quote 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. But, also check: ReplaceList[{1,2,3,4,5,6}, {a___, b_, c_, d___} /; (c > b) -> {a,f@ b,g@ c,d}] 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. W. "Daohua Song" <ds2081 at columbia.edu> wrote in message news:ctcrok$6r2$1 at smc.vnet.net... > 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 > DHSONG >