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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With