I define a function which counts the number of negative elements in a list as follows:
g[x1_, x2_] := Count[{x1, x2}, _?Negative]
Plugging in values shows that it does what it's supposed to do
g[1, -2]
returns 1
For some reason using replacement rules doesn't work as expected
g[x1, x2] /. {x1 -> 1, x2 -> -2}
returns 0
Thanks for the answers. I am not quite clear on why Count behaves differently from any other function. If I define
g[x1_,x2_]:= x1+x2
it works just fine. Why is in the case of Count anything evaluated before it gets values in the first place? Is this a bug? I thought that using set delayed (:=) in my function definition would prevent the RHS to be evaluated?
g[x1, x2] /. {x1 -> 1, x2 -> -2} // Trace
to see what is happening. $\endgroup$Unevaluated[g[x1, x2]] /. {x1 -> 1, x2 -> -2}
to get1
. $\endgroup$g @@ ({x1, x2} /. {x1 -> 1, x2 -> -2})
$\endgroup$