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

3

5

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?

Bally answered 29/12, 2014 at 19:46 Comment(3)
How would you create an instance of that type?Lewan
You can find non-empty lists in the semigroups package.Oina
A more general answer is, that Haskell grew out of much more mundane means. It grew much much better over time. But changing the core libraries that all code relies on afterwards is a bad thing, no matter how much better the new stuff is. … The current solution to this is that there are alternate Preludes. … Which, thanks to advanced compiler extensions, allow seemingly built-in syntax and literals in your code to be redefined to use better types. I highly recommend using those as early as possible, so you don’t have to unlearn certain things.Scurlock
E
11

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.

Escuage answered 29/12, 2014 at 20:6 Comment(1)
can you provide an example and show how NonEmpty is used?Panfish
G
7

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

Gee answered 29/12, 2014 at 19:56 Comment(0)
P
6

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

Pollak answered 27/5, 2016 at 19:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.