[Haskell-beginners] Type for lists with one or more elements

Brent Yorgey byorgey at seas.upenn.edu
Fri Jun 7 02:56:09 CEST 2013


On Thu, Jun 06, 2013 at 05:27:11PM -0700, Frerich Raabe wrote:
> Hi,
> 
> is there an existing (or conventional) way to model lists of one or
> more items? I often deal with functions with don't make sense with
> empty lists, and the caller already verified that the lists are not
> empty. So to avoid getting warnings from ghc, I'd like to have a
> dedicated type for this case.
> 
> I could do something like
> 
>   data List a = Singleton a | Cons a (List a)
> 
> but before I go ahead I wonder: is there an existing Haskell package
> for this? Maybe even with prettier names than what I wrote above, and
> convenient functions for transforming from/to plain [] lists? It
> would probably be straightforward to write it myself, but because of
> that, I suspect that somebody else already did it. :-)

Yes, this exists in the semigroups package:

  http://hackage.haskell.org/packages/archive/semigroups/0.9.2/doc/html/Data-List-NonEmpty.html

-Brent



More information about the Beginners mailing list