Repeated (pattn ..), RepeatedNull (pattn ...)


Repeated (pattn ..) is used to represent a sequence of arguments that match a
certain pattern.  For example in the next cell (f) is defined when given a
sequence of one or more arguments that have the head Real or Integer.  When
Repeated (..) is used the definition applies when some of the arguments are
Real and others are Integer.

Clear[f, s]  f[x : (_Real | _Integer) ..] := Join[{x}, {∞}]  {f[2], f[2.4], f[2, 4.3], f[], f[2, s]}

{{2, ∞}, {2.4, ∞}, {2, 4.3, ∞}, f[], f[2, s]}


In the previous example (f) was only defined when given one or more arguments
that are Real or Integer.  NullRepeated (...) represents a sequence multiple,
one, or an empty set of arguments matching a pattern.  So in the next example
(f) is defined when given no arguments.

Clear[f, s]  f[x : (_Real | _Integer) ...] := Join[{x}, {∞}]  {f[2], f[2.4], f[2, 4.3], f[], f[2, s]}

{{2, ∞}, {2.4, ∞}, {2, 4.3, ∞}, {∞}, f[2, s]}


Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page