Re: Bug in pattern test, or I did something wrong?
- To: mathgroup at smc.vnet.net
- Subject: [mg125951] Re: Bug in pattern test, or I did something wrong?
- From: A Retey <awnl at gmx-topmail.de>
- Date: Mon, 9 Apr 2012 05:33:36 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <201204050951.FAA13156@smc.vnet.net> <jlmejj$m99$1@smc.vnet.net>
Hi, > Thank you all (especially Bob) for the kind suggestions! Bob's last > post completely solves the problem. Here is a brief summary for > people's suggestions and what works, in case for later reference: > > (1) Problem: > > f[a_ /; MemberQ[{0, 1, 2}, a]] := 0; > f[x] /. f[expr_] :> f[-expr] > (* gets f[x] instead of f[-x] *) > > (2) I posted a workaround, but that's wrong (thank Fred to point it > out). I should instead write > > g[a_?(MemberQ[{0,1,2},#]&)]:=0; > g[x]/.g[expr_]:>g[-expr] > (* still gets g[x] instead of g[-x] *) > > (3) The elegant and complete solution by Bob (as explained in his post): > > f[x] /. HoldPattern[f[expr_]] :> f[-expr] > > (4) A workaround by Christoph: > > Do[h[i] = 0, {i, 0, 2}] > > (5) I also found workaround: > > hasQ[list_,var_]:=Or@@Map[(var===# || Head[var]===#)&,list]; > And use hasQ instead of MemberQ You are missing the solution which I think is probably the most simple. Use Alternatives: ClearAll[f]; f[0|1|2]=0; f[x] /. f[expr_] :> f[-expr] I used a version without giving the pattern a name, since there is not much sense doing so if you don't use that name anywhere. In case that example is just a simplification you can of course use something like, e.g.: f[a:(0|1|2)]:=a^2 hth, albert
- References:
- Bug in pattern test, or I did something wrong?
- From: Yi Wang <tririverwangyi@gmail.com>
- Bug in pattern test, or I did something wrong?