Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no "non-empty list" type in the Haskell base libraries?

This type could be

data NonEmptyList a = NEL a [a]

The functions head, tail, and others will become methods of a newly created Listable type class. Some functions can already fit in an existing type class (maps/folds/traversals/monads).

Why is such a type not part of the Haskell standard library?

like image 898
tohava Avatar asked Dec 29 '14 19:12

tohava


2 Answers

It is in base now since GHC 8.0: https://hackage.haskell.org/package/base-4.9.0.0/docs/Data-List-NonEmpty.html


The list of packages that define such a type is itself rather nonempty: there are at least six of them:

  • NonEmpty
  • NonEmptyList
  • Cardinality
  • non-empty
  • semigroups
  • mono-traversable

The Haskell Wiki has a whole page about non-empty lists.

Your question: why are non-empty lists not in the base package is more difficult to answer. But the type is an instance of many useful classes from base (Foldable, Zip) so the machinery for using them is there already, and you need just a small number of instance definitions to use that.

like image 160
Hans Lub Avatar answered Oct 19 '22 10:10

Hans Lub


The type actually exists.

You have to import

Data.List.NonEmpty    

More info : http://hackage.haskell.org/package/semigroups-0.16.0.1/docs/Data-List-NonEmpty.html

like image 7
Franklin S. Avatar answered Oct 19 '22 11:10

Franklin S.