# How to get f[x] to match with f[c_., x] when OneIdentity doesn't match

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] *).

• I do know that OneIdentity is a little weird. See here. Mar 14 '16 at 16:52
• But as far as your "something spooky", since all of f[x], f[f[x]] are the same as x for the purpose of pattern matching, perhaps x matches the pattern f[n_., y_], and so it gets replaced by f[x]. Mar 14 '16 at 16:58
• Yes: Try x /. f[y_, n_.] :> f[y, n]. Mar 14 '16 at 16:59
• as a workaround simply do MatchQ[f[x], f[n_., x] | f[x]] Mar 14 '16 at 18:26
• By the way, every single one of your MatchQ's return True when f does not have the OneIdentity Attribute, which means the only thing OneIdentity is doing is changing MatchQ[f[x], f[n_. , x]] from True to False. Mar 14 '16 at 20:44