       Re: Bug in pattern test, or I did something wrong?

• To: mathgroup at smc.vnet.net
• Subject: [mg125911] Re: Bug in pattern test, or I did something wrong?
• From: Christoph Lhotka <christoph.lhotka at fundp.ac.be>
• Date: Fri, 6 Apr 2012 06:01:13 -0400 (EDT)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com
• References: <201204050951.FAA13156@smc.vnet.net>

```hi,

this is an interesting problem.

The reason why your first replacement rule does not work is hidden in
the evaluation process.

Define

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

As you mention you get:

In:= f[x] /. f[expr_] :> f[-expr]

Out= f[x]

Use Trace to see the reason:

In:= Trace[f[x] /. f[expr_] :> f[-expr]]

Out:=
{{f[x],{MemberQ[{0,1,2},x],False},f[x]},{{f[expr_],{MemberQ[{0,1,2},expr_],True},0},0:>f[-expr],0:>f[-expr]},f[x]/.
0:>f[-expr],f[x]}

f[x] evaluates to f[x] while f[expr_] evaluates to 0 thus f[x] /. 0 :>
f[-expr] does not apply
and you get f[x].

So the problem is the following:

In:= f[expr_]

Out= 0

Which is due to MemberQ

In:= MemberQ[{0, 1, 2}, expr_]

Out= True

The evaluation chain is ok:

In:= 0 /. f[expr_] :> f[-expr]

Out= f[-expr]

By the way, in your definition of g

In:= g[a_?MemberQ[{0, 1, 2}, #] &] := 0

the replacement works

In:= g[x] /. g[expr_] :> g[-expr]

Out= g[-x]

but not the definition:

In:= g

Out= g

If I understand your needs well a simple solution to your problem would be:

In:= Do[h[i] = 0, {i, 0, 2}]

which gives:

In:= {h, h, h, h[x]} /. h[expr_] :> h[-expr]

Out:= {0, 0, 0, h[-x]}

```

• Prev by Date: Re: Extract XLM data from in Mathematica
• Next by Date: Re: gives
• Previous by thread: Bug in pattern test, or I did something wrong?
• Next by thread: Re: Bug in pattern test, or I did something wrong?