I have the following line of code:
Plus @@ Table[p[x], {x, 0, 20}] /. p[x_] -> Boole[MemberQ[{0, 5}, x]]
The first part of this produces
p[0] + p[1] + p[2] + p[3] + p[4] + p[5] + p[6] + p[7] + p[8] + p[9] + p[10] +
p[11] + p[12] + p[13] + p[14] + p[15] + p[16] + p[17] + p[18] + p[19] + p[20]
What I thought the second part would do is to check each argument to p that occurs in this sum and replace p[x]
with 1
if x
is either 0
or 5
and with 0
if x
is anything else. I thought, in short, the result would be 2. That's not, however, what I get. Instead Mathematica returns 0. What am I doing wrong?
->
with:>
;) (you want the function on the right to evaluate on substitution and not when defining the rule, this is called a delayed rule, you can find more on the help.) $\endgroup$