HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulerelude-1.2.0.0Haskell2010

Relude.List.NonEmpty

SPDX-License-Identifier : MIT Maintainer : Kowainik xrom.xkov@gmail.com Stability : Stable Portability : Portable

This module contains reexports from Data.List.NonEmpty and safe functions to work with list type in terms of NonEmpty.

Note, that Relude reexports head, tail, init, last from Data.List.NonEmpty instead of the Data.List, so these functions are safe to use.

base

relude

head

[a] -> a

NonEmpty a -> a

tail

[a] -> [a]

NonEmpty a -> [a]

last

[a] -> a

NonEmpty a -> a

init

[a] -> [a]

NonEmpty a -> [a]

relude also provides custom type error for better experience with transition from lists to NonEmpty with those functions.

Let's examine the behaviour of the relude list functions comparing to the corresponding base one on the example of the head function:

head

base

[a] -> a

relude

NonEmpty a -> a

Example with list

base

> head [1..5]

1

Example with empty list

base

> head []

*** Exception: Prelude.head: empty list

Example with

NonEmpty

relude

> head $ 1 :| [2..5]

1

Example with list

relude

> viaNonEmpty head [1..5]

Just 1

Example with empty list

relude

> viaNonEmpty head []

Nothing

  • 1 type
  • 8 values
  • Packagerelude-1.2.0.0
  • Exports9
  • LanguageHaskell2010
  • LicenceMIT
  • SourceNonEmpty.hs

Reexports from DataList.NonEmpty

6 declarations
datadata NonEmpty a
#

Non-empty (and non-strict) list type.

Constructors

  • a :| [a]infixr 5
Instances33Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
valuehead :: IsNonEmpty f a a "head" => f a -> a
#

O(1). Extracts the first element of a NonEmpty list.

Actual type of this function is the following:

head :: NonEmpty a -> a

but it was given a more complex type to provide friendlier compile time errors.

Example3 expressions
head ('a' :| "bcde")'a'head [0..5 :: Int]...... 'head' works with 'NonEmpty', not ordinary lists.      Possible fix:          Replace: [Int]          With:    NonEmpty Int...      However, you can use 'head' with the ordinary lists.      Apply 'viaNonEmpty' function from relude:          viaNonEmpty head (yourList)      Note, that this will return 'Maybe Int'      therefore it is a safe function unlike 'head' from the standard Prelude...head (Just 'a')...... 'head' works with 'NonEmpty Char' lists      But given: Maybe Char...
valuetail :: IsNonEmpty f a [a] "tail" => f a -> [a]
#

O(1). Return all the elements of a NonEmpty list after the head element.

Actual type of this function is the following:

tail :: NonEmpty a -> [a]

but it was given a more complex type to provide friendlier compile time errors.

Example3 expressions
tail ('a' :| "bcde")"bcde"tail [0..5 :: Int]...... 'tail' works with 'NonEmpty', not ordinary lists.      Possible fix:          Replace: [Int]          With:    NonEmpty Int...      However, you can use 'tail' with the ordinary lists.      Apply 'viaNonEmpty' function from relude:          viaNonEmpty tail (yourList)      Note, that this will return 'Maybe [Int]'      therefore it is a safe function unlike 'tail' from the standard Prelude...tail (Just 'a')...... 'tail' works with 'NonEmpty Char' lists      But given: Maybe Char...
valuelast :: IsNonEmpty f a a "last" => f a -> a
#

O(n). Extracts the last element of a NonEmpty list.

Actual type of this function is the following:

last :: NonEmpty a -> a

but it was given a more complex type to provide friendlier compile time errors.

Example3 expressions
last ('a' :| "bcde")'e'last [0..5 :: Int]...... 'last' works with 'NonEmpty', not ordinary lists.      Possible fix:          Replace: [Int]          With:    NonEmpty Int...      However, you can use 'last' with the ordinary lists.      Apply 'viaNonEmpty' function from relude:          viaNonEmpty last (yourList)      Note, that this will return 'Maybe Int'      therefore it is a safe function unlike 'last' from the standard Prelude...last (Just 'a')...... 'last' works with 'NonEmpty Char' lists      But given: Maybe Char...
valueinit :: IsNonEmpty f a [a] "init" => f a -> [a]
#

O(n). Return all the elements of a NonEmpty list except the last one element.

Actual type of this function is the following:

init :: NonEmpty a -> [a]

but it was given a more complex type to provide friendlier compile time errors.

Example3 expressions
init ('a' :| "bcde")"abcd"init [0..5 :: Int]...... 'init' works with 'NonEmpty', not ordinary lists.      Possible fix:          Replace: [Int]          With:    NonEmpty Int...      However, you can use 'init' with the ordinary lists.      Apply 'viaNonEmpty' function from relude:          viaNonEmpty init (yourList)      Note, that this will return 'Maybe [Int]'      therefore it is a safe function unlike 'init' from the standard Prelude...init (Just 'a')...... 'init' works with 'NonEmpty Char' lists      But given: Maybe Char...

Combinators

3 declarations
valueviaNonEmpty :: (NonEmpty a -> b) -> [a] -> Maybe b
#

For safe work with lists using functions for NonEmpty.

Example2 expressions
viaNonEmpty head [1]Just 1viaNonEmpty head []Nothing
valuewhenNotNull :: Applicative f => [a] -> (NonEmpty a -> f ()) -> f ()
#

Performs given action over NonEmpty list if given list is non empty.

Example2 expressions
whenNotNull [] $ \(b :| _) -> print (not b)whenNotNull [False,True] $ \(b :| _) -> print (not b)True