tohava
tohava

Reputation: 5412

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?

Upvotes: 5

Views: 2685

Answers (3)

Hans Lub
Hans Lub

Reputation: 5678

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:

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.

Upvotes: 11

Gabriella Gonzalez
Gabriella Gonzalez

Reputation: 35089

As of GHC 8.0.1, base now has a NonEmpty list type in Data.List.NonEmpty:

https://hackage.haskell.org/package/base-4.9.0.0/docs/Data-List-NonEmpty.html

Upvotes: 6

Franklin S.
Franklin S.

Reputation: 71

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

Upvotes: 7

Related Questions