After setting:
SetAttributes[f, OneIdentity]
Default[f] = 1;
We have the following successful matches:
MatchQ[f[x, y, z, a], f[n_. , x, y, z, a]] (* True *)
MatchQ[f[x, y, z], f[n_. , x, y, z]] (* True *)
MatchQ[f[x, y], f[n_. , x, y]] (* True *)
but
MatchQ[f[x], f[n_. , x]] (* False *)
What do I need to do so that the case with one argument matches?
Note: something spooky is happening:
f[x] /. f[n_., y_] :> f[n, y]
(* f[1, f[x]] *)
instead of (* f[1, x] *).
OneIdentityis a little weird. See here. – march Mar 14 '16 at 16:52f[x],f[f[x]]are the same asxfor the purpose of pattern matching, perhapsxmatches the patternf[n_., y_], and so it gets replaced byf[x]. – march Mar 14 '16 at 16:58x /. f[y_, n_.] :> f[y, n]. – march Mar 14 '16 at 16:59MatchQ[f[x], f[n_., x] | f[x]]– george2079 Mar 14 '16 at 18:26MatchQ[f[x], f[n_., f[x]]](works for multi arg cases as well ). – george2079 Mar 14 '16 at 18:51MatchQ's returnTruewhenfdoes not have theOneIdentityAttribute, which means the only thingOneIdentityis doing is changingMatchQ[f[x], f[n_. , x]]fromTruetoFalse. – march Mar 14 '16 at 20:44