Repeat a value
n times.
mtimesDefault n a = a <> a <> ... <> a -- using <> (n-1) times
In many cases,
stimes 0 a for a
Monoid will
produce
mempty. However, there are situations when it cannot do
so. In particular, the following situation is fairly common:
data T a = ...
class Constraint1 a
class Constraint1 a => Constraint2 a
instance Constraint1 a => Semigroup (T a)
instance Constraint2 a => Monoid (T a)
Since
Constraint1 is insufficient to implement
mempty,
stimes for
T a cannot do so.
When working with such a type, or when working polymorphically with
Semigroup instances,
mtimesDefault should be used when
the multiplier might be zero. It is implemented using
stimes
when the multiplier is nonzero and
mempty when it is zero.
Examples
>>> mtimesDefault 0 "bark"
""
>>> mtimesDefault 3 "meow"
"meowmeowmeow"