With SetAttributes[f, Flat], f[a_] can get matched to f of any number arguments. How can I make it that calling it with exactly one argument f[x] returns x?

I'm trying to have an operation just like NonCommutativeMultiply but without the "Possible Issues" it has. It is important to me that if the result of some simplification is a "product" of one term it is no longer treated as such.

I especially want to avoid infinite loops like

f[a_] := a
f[a__] /; AnyTrue[{a}, FreeQ[_Symbol]] := "free"
f[a, a]

$IterationLimit::itlim: Iteration limit of 4096 exceeded. >>

  • $\begingroup$ use SetAttributes[f, Flat] after f[a_] := a? $\endgroup$
    – kglr
    Commented Mar 10, 2020 at 10:44
  • 2
    $\begingroup$ closely related/possible duplicate: SetAttributes: Why the order dependence? $\endgroup$
    – kglr
    Commented Mar 10, 2020 at 10:47


Browse other questions tagged or ask your own question.