I was trying to define a function f
such that, when its second argument is negative, we have
$$
f(a,b,c,d,\dots,n)=f(a,-b,-c,-d,\dots,-n)
$$
i.e., we reverse the sign of everything except for its first argument. The shortest code I could come up with is
f[a_, b__] /; Negative[{b}[[1]]] := -f[a, Sequence @@ Minus /@ {b}]
which is admittedly not very clean (is there a better approach?).
But anyway, for fun, my first attempt was
f[a_, b__] /; Negative[{b}[[1]]] := f[a, -b]
which I didn't really expect to work. Much to my surprise, this code does not throw any errors, but it does not really do what I want:
f[1, -2, 3, 4]
(* f[1, 24] *)
which means that $$ f(a,b,c,d,\dots,n)=f(a,-bcd\cdots n) $$
What is going on here? I thought that -b
would be interpreted as -(-2,3,4)
(which, as I expected, throws an error). But Trace
ing it, it seems that it is interpreted as -(-2)*3*4
. Why?
-b // FullForm
$\endgroup$