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


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

  • $\begingroup$ I do know that OneIdentity is a little weird. See here. $\endgroup$
    – march
    Mar 14 '16 at 16:52
  • 1
    $\begingroup$ 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]. $\endgroup$
    – march
    Mar 14 '16 at 16:58
  • $\begingroup$ Yes: Try x /. f[y_, n_.] :> f[y, n]. $\endgroup$
    – march
    Mar 14 '16 at 16:59
  • $\begingroup$ as a workaround simply do MatchQ[f[x], f[n_., x] | f[x]] $\endgroup$
    – george2079
    Mar 14 '16 at 18:26
  • 3
    $\begingroup$ 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. $\endgroup$
    – march
    Mar 14 '16 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.