[Haskell-cafe] Please add a method for optimized concat to the Semigroup class

Yitzchak Gale gale at sefer.org
Tue May 3 14:26:56 CEST 2011

Stephen Tetley wrote:
> There is that formulation, though usually I find I need to do it with
> an alternative instead:
> altconcat alt []     = alt
> altconcat _   (a:as) = go a as
>  where
>    go acc [] = acc
>    go acc (b:bs) = go (acc <> b) bs

But the whole reason we need this as a method is
for the case that consecutive appends is inefficient.

> Both are "kind of, sort of" bringing you up to a Monoid though...

altconcat and sconcatMaybe are doing that, because you
need to decide what to do with an empty list when you
define the instance. Holger's interface is not doing that,
because the type does not require you to say anything
about the case of an empty list in the instance.

Another approach would be to depend on one of the
packages that provides a non-empty list type, such as
the NonEmptyList package. But I don't think this simple
case justifies another dependency. You can wrap Holger's
function in one of those types easily enough if you need to.


More information about the Haskell-Cafe mailing list