Flat

Flat

is an attribute that can be assigned to a symbol f to indicate that all expressions involving nested functions f should be flattened out. This property is accounted for in pattern matching.

Details

  • Flat corresponds to the mathematical property of associativity.
  • For a symbol f with attribute Flat, f[f[a,b],f[c]] is automatically reduced to f[a,b,c].
  • Functions like Plus, Times, and Dot are Flat.
  • For a Flat function f, the variables x and y in the pattern f[x_,y_] can correspond to any sequence of arguments.
  • The Flat attribute must be assigned before defining any values for a Flat function.

Examples

open allclose all

Basic Examples  (3)

Nested expressions with flat functions are flattened out:

In[1]:=
Click for copyable input
In[2]:=
Click for copyable input
Out[2]=

Flat implements the notion of associativity:

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=

Flat allows the pattern matcher to use associativity:

In[1]:=
Click for copyable input
Out[1]=

Scope  (3)

Properties & Relations  (5)

Possible Issues  (4)

See Also

Orderless  OneIdentity

Tutorials

Introduced in 1988
(1.0)