4

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

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
QuantumDot
  • 19,601
  • 7
  • 45
  • 121
  • I do know that OneIdentity is a little weird. See here. – march Mar 14 '16 at 16:52
  • 1
    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]. – march Mar 14 '16 at 16:58
  • Yes: Try x /. f[y_, n_.] :> f[y, n]. – march Mar 14 '16 at 16:59
  • as a workaround simply do MatchQ[f[x], f[n_., x] | f[x]] – george2079 Mar 14 '16 at 18:26
  • another thing that works: MatchQ[f[x], f[n_., f[x]]] (works for multi arg cases as well ). – george2079 Mar 14 '16 at 18:51
  • 3
    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. – march Mar 14 '16 at 20:44

0 Answers0