This works:
(w[1] + w[2] + w[3]) /. w[s_] -> If[s == 2, w[s], 0]
(* w[2] *)
This doesn't:
(w[1] + w[2] + w[3]) /. w[s_] -> If[EvenQ[s], w[s], 0]
(* 0 *)
Why?
This works:
(w[1] + w[2] + w[3]) /. w[s_] -> If[s == 2, w[s], 0]
(* w[2] *)
This doesn't:
(w[1] + w[2] + w[3]) /. w[s_] -> If[EvenQ[s], w[s], 0]
(* 0 *)
Why?
The second one does not work as intended because EvenQ[x] always returns False if x is not "manifestly an even integer" (quote from docs.) Since you use -> instead of :>, the expression If[EvenQ[s], w[s], 0] is evaluated at once, and since s is a symbol at that stage, it is not manifestly an even integer. The first example works because s == 2 is neither True nor False until s aquires a value, which happens after replacement. The second one works with RuleDelayed:
(w[1] + w[2] + w[3]) /. w[s_] :> If[EvenQ[s], w[s], 0]
(* w[2] *)
=/:= and ->/:>; the ones on the left evaluate their right-hand sides at once, and the right ones don't. In your case, the instant evaluation resulted in an If[False, (* stuff *)].
– J. M.'s missing motivation
Jul 22 '15 at 05:07
Equal does not evaluate at once unless both sides are numeric or manifestly equal, which is why it is useful for representing equations (e.g. in Solve[] and ContourPlot[]).
– J. M.'s missing motivation
Jul 22 '15 at 06:57
= and :=.
– Marius Ladegård Meyer
Jul 25 '15 at 11:31
RuleDelayed(:>) – ciao Jul 22 '15 at 04:52