H[z_] := z /. {F[p__] -> F[Reverse[p]]}
F[{x, y}] + F[{x, z}] + F[{}] // H
Almost. It gives an annoying "Nonatomic Reverse" error message at the first call of H - why only the first?!? - and moreover, I'd like to have this without the lists, i.e. an occurence of, say, F[X,Y]
is replaced by F[Y,X]
. To my dismay, the following does not work!
G[z_] := z /. {F[p__] -> ReverseApplied[F][p]}
F[x, y] + F[x, z] + F[] // G
Neither does any other argument reversal of F
(e.g. recursive) "bite" in the pattern matching. What is going on? I mean, any p__
found inside a F
first gets replace by a Sequence[p]
but then everything goes haywire. It seems to be an operation order problem, Sequence
is executed too late?!
EDIT: I tried out the tip, but it still doesn't seem to reverse. The following does work:
G[z_] := z /. {F[p___] -> Hold[ReverseApplied[F][p]]]} // ReleaseHold;
It is an order problem! Can someone explain?
Reverse[F[p]]
$\endgroup$