Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2012

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

Search the Archive

Bug in pattern test, or I did something wrong?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg125862] Bug in pattern test, or I did something wrong?
  • From: Yi Wang <tririverwangyi at gmail.com>
  • Date: Thu, 5 Apr 2012 05:51:18 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com

Hi, all,

When I define a function using pattern test conditions, I got some unexpected Replace (or ReplaceAll) behaviour:

ClearAll[f, g, a, x];
f[a_ /; MemberQ[{0, 1, 2}, a]] := 0;

f[x] /. f[expr_] :> f[-expr] 
(* expect f[-x], but I got f[x] *)

f[x] /. f[expr_] + a_. :> f[-expr] + a
(* Even worse, Here I got f[-expr] + f[x], completely weird! *)

PS: Workaround: if I use another form of pattern test, there is no problem:

g[a_?MemberQ[{0, 1, 2}, #] &] := 0;
g[x] /. g[expr_] :> g[-expr]

(* g[-x] as desired *)

g[x] /. g[expr_] + a_. :> g[-expr] + a
(* g[-x] as desired *)



  • Prev by Date: Re: addRule
  • Next by Date: Re: Pattern Syntax for Alternate Heads with Default Value
  • Previous by thread: Re: fyi, small note on using Mathematica for object based programming
  • Next by thread: Re: Bug in pattern test, or I did something wrong?