2
$\begingroup$

The documentation on the attribute OneIdentity says

OneIdentity is an attribute that can be assigned to a symbol f to indicate that f[x], f[f[x]], etc. are all equivalent to x for the purpose of pattern matching.

Then why does the input

SetAttributes[f,OneIdentity]
MatchQ[x, f[_]]

return False? On the other hand (from the examples in the documentation), the following input returns True:

SetAttributes[f, OneIdentity]
MatchQ[a, f[x_: 0, y_]]

Is there a problem with the documentation or am I misunderstanding it?

$\endgroup$
4
  • $\begingroup$ What happens if you use f[__] (BlankSequence instead of Blank)? $\endgroup$
    – J. M.'s torpor
    Jul 20 '15 at 12:06
  • 4
    $\begingroup$ See here for an answer to your question. $\endgroup$ Jul 20 '15 at 12:14
  • $\begingroup$ @Guesswhoitis. Still returns false. $\endgroup$
    – Praan
    Jul 20 '15 at 12:24
  • $\begingroup$ I think the definition given in the answer of the linked question is slightly wrong. For example, MatchQ[a,f[x_: 0, y_, __]] returns False even though there is one optional argument in the first two pattern argument positions (as stated in the linked answer). A more correct definition would be: there has to be at least one optional argument in f[...] and besides optional arguments there can be at most one argument that is not optional. $\endgroup$
    – Praan
    Jul 21 '15 at 10:12