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

Modulelinear-base-0.4.0Haskell2010

Data.List.Linear

Linear versions of Data.List functions.

This module only contains minimal amount of documentation; consult the original Data.List module for more detailed information.

  • 59 values

Basic functions

13 declarations
value(++) :: [a] %1 -> [a] %1 -> [a]
#
valuemap :: (a %1 -> b) -> [a] %1 -> [b]
#
valuefilter :: Dupable a => (a %1 -> Bool) -> [a] %1 -> [a]
#

filter p xs returns a list with elements satisfying the predicate.

See mapMaybe if you do not want the Dupable constraint.

valuehead :: HasCallStack => [a] -> a
#

This is a partial function, it throws an error on empty lists. Use pattern matching, uncons or listToMaybe instead. Consider refactoring to use Data.List.NonEmpty.

\mathcal{O}(1). Extract the first element of a list, which must be non-empty.

To disable the warning about partiality put {-# OPTIONS_GHC -Wno-x-partial -Wno-unrecognised-warning-flags #-} at the top of the file. To disable it throughout a package put the same options into ghc-options section of Cabal file. To disable it in GHCi put :set -Wno-x-partial -Wno-unrecognised-warning-flags into ~/.ghci config file. See also the migration guide.

Examples
Example1 expression
head [1, 2, 3]1
Example1 expression
head [1..]1
Example1 expression
head []*** Exception: Prelude.head: empty list
valuetail :: HasCallStack => [a] -> [a]
#

This is a partial function, it throws an error on empty lists. Replace it with drop 1, or use pattern matching or uncons instead. Consider refactoring to use Data.List.NonEmpty.

\mathcal{O}(1). Extract the elements after the head of a list, which must be non-empty.

To disable the warning about partiality put {-# OPTIONS_GHC -Wno-x-partial -Wno-unrecognised-warning-flags #-} at the top of the file. To disable it throughout a package put the same options into ghc-options section of Cabal file. To disable it in GHCi put :set -Wno-x-partial -Wno-unrecognised-warning-flags into ~/.ghci config file. See also the migration guide.

Examples
Example1 expression
tail [1, 2, 3][2,3]
Example1 expression
tail [1][]
Example1 expression
tail []*** Exception: Prelude.tail: empty list
valuelast :: HasCallStack => [a] -> a
#

\mathcal{O}(n). Extract the last element of a list, which must be finite and non-empty.

WARNING: This function is partial. Consider using unsnoc instead.

Examples
Example1 expression
last [1, 2, 3]3
Example1 expression
last [1..]* Hangs forever *
Example1 expression
last []*** Exception: Prelude.last: empty list
valueinit :: HasCallStack => [a] -> [a]
#

\mathcal{O}(n). Return all the elements of a list except the last one. The list must be non-empty.

WARNING: This function is partial. Consider using unsnoc instead.

Examples
Example1 expression
init [1, 2, 3][1,2]
Example1 expression
init [1][]
Example1 expression
init []*** Exception: Prelude.init: empty list
valuelookup :: Eq a => a -> [(a, b)] -> Maybe b
#

\mathcal{O}(n). lookup key assocs looks up a key in an association list. For the result to be Nothing, the list must be finite.

Examples
Example1 expression
lookup 2 []Nothing
Example1 expression
lookup 2 [(1, "first")]Nothing
Example1 expression
lookup 2 [(1, "first"), (2, "second"), (3, "third")]Just "second"
valuelength :: [a] %1 -> (Ur Int, [a])
#

Return the length of the given list alongside with the list itself.

methodnull :: t a -> Bool
#

Test whether the structure is empty. The default implementation is Left-associative and lazy in both the initial element and the accumulator. Thus optimised for structures where the first element can be accessed in constant time. Structures where this is not the case should have a non-default implementation.

Examples

Basic usage:

Example1 expression
null []True
Example1 expression
null [1]False

null is expected to terminate even for infinite structures. The default implementation terminates provided the structure is bounded on the left (there is a leftmost element).

Example1 expression
null [1..]False

Extracting sublists

11 declarations
valuetake :: Consumable a => Int -> [a] %1 -> [a]
#

NOTE: This does not short-circuit and always traverses the entire list to consume the rest of the elements.

valuespan :: Dupable a => (a %1 -> Bool) -> [a] %1 -> ([a], [a])
#

span, applied to a predicate p and a list xs, returns a tuple where first element is longest prefix (possibly empty) of xs of elements that satisfy p and second element is the remainder of the list.

valuetakeWhile :: Dupable a => (a %1 -> Bool) -> [a] %1 -> [a]
#

NOTE: This does not short-circuit and always traverses the entire list to consume the rest of the elements.

valuefind :: Foldable t => (a -> Bool) -> t a -> Maybe a
#

The find function takes a predicate and a structure and returns the leftmost element of the structure matching the predicate, or Nothing if there is no such element.

Examples

Basic usage:

Example1 expression
find (> 42) [0, 5..]Just 45
Example1 expression
find (> 12) [1..7]Nothing
valueintersperse :: a -> [a] %1 -> [a]
#

The intersperse function takes an element and a list and intersperses that element between the elements of the list.

valueintercalate :: [a] -> [[a]] %1 -> [a]
#

intercalate xs xss is equivalent to (concat (intersperse xs xss)). It inserts the list xs in between the lists in xss and concatenates the result.

valuetranspose :: [[a]] %1 -> [[a]]
#

The transpose function transposes the rows and columns of its argument.

Folds

8 declarations
valuefoldl :: (b %1 -> a %1 -> b) -> b %1 -> [a] %1 -> b
#
valuefoldl' :: (b %1 -> a %1 -> b) -> b %1 -> [a] %1 -> b
#
valuefoldr :: (a %1 -> b %1 -> b) -> b %1 -> [a] %1 -> b
#
valuefoldMap :: Monoid m => (a %1 -> m) -> [a] %1 -> m
#

Map each element of the structure to a monoid, and combine the results.

valuefoldMap' :: Monoid m => (a %1 -> m) -> [a] %1 -> m
#

A variant of foldMap that is strict in the accumulator.

Special folds

8 declarations
valueconcat :: [[a]] %1 -> [a]
#
valueconcatMap :: (a %1 -> [b]) -> [a] %1 -> [b]
#
valueand :: [Bool] %1 -> Bool
#

NOTE: This does not short-circuit, and always consumes the entire container.

valueor :: [Bool] %1 -> Bool
#

NOTE: This does not short-circuit, and always consumes the entire container.

valueany :: (a %1 -> Bool) -> [a] %1 -> Bool
#

NOTE: This does not short-circuit, and always consumes the entire container.

valueall :: (a %1 -> Bool) -> [a] %1 -> Bool
#

NOTE: This does not short-circuit, and always consumes the entire container.

Building lists

9 declarations
valuescanl :: Dupable b => (b %1 -> a %1 -> b) -> b %1 -> [a] %1 -> [b]
#
valuescanl1 :: Dupable a => (a %1 -> a %1 -> a) -> [a] %1 -> [a]
#
valuescanr :: Dupable b => (a %1 -> b %1 -> b) -> b %1 -> [a] %1 -> [b]
#
valuescanr1 :: Dupable a => (a %1 -> a %1 -> a) -> [a] %1 -> [a]
#
valuerepeat :: Dupable a => a %1 -> [a]
#

Deprecated. The result cannot be consumed linearly, so this function is not useful.

valuecycle :: (HasCallStack, Dupable a) => [a] %1 -> [a]
#

Deprecated. The result cannot be consumed linearly, so this function is not useful.

valueiterate :: Dupable a => (a %1 -> a) -> a %1 -> [a]
#

Deprecated. The result cannot be consumed linearly, so this function is not useful.

Ordered lists

3 declarations
valuesort :: Ord a => [a] -> [a]
#

The sort function implements a stable sorting algorithm. It is a special case of sortBy, which allows the programmer to supply their own comparison function.

Elements are arranged from lowest to highest, keeping duplicates in the order they appeared in the input.

The argument must be finite.

Examples
Example1 expression
sort [1,6,4,3,2,5][1,2,3,4,5,6]
Example1 expression
sort "haskell""aehklls"
Example2 expressions
import Data.Semigroup(Arg(..))sort [Arg ":)" 0, Arg ":D" 0, Arg ":)" 1, Arg ":3" 0, Arg ":D" 1][Arg ":)" 0,Arg ":)" 1,Arg ":3" 0,Arg ":D" 0,Arg ":D" 1]
valuesortOn :: Ord b => (a -> b) -> [a] -> [a]
#

Sort a list by comparing the results of a key function applied to each element. sortOn f is equivalent to sortBy (comparing f), but has the performance advantage of only evaluating f once for each element in the input list. This is called the decorate-sort-undecorate paradigm, or Schwartzian transform.

Elements are arranged from lowest to highest, keeping duplicates in the order they appeared in the input.

The argument must be finite.

Examples
Example1 expression
sortOn fst [(2, "world"), (4, "!"), (1, "Hello")][(1,"Hello"),(2,"world"),(4,"!")]
Example1 expression
sortOn length ["jim", "creed", "pam", "michael", "dwight", "kevin"]["jim","pam","creed","kevin","dwight","michael"]
Performance notes

This function minimises the projections performed, by materialising the projections in an intermediate list.

For trivial projections, you should prefer using sortBy with comparing, for example:

Example1 expression
sortBy (comparing fst) [(3, 1), (2, 2), (1, 3)][(1,3),(2,2),(3,1)]

Or, for the exact same API as sortOn, you can use `sortBy . comparing`:

Example1 expression
(sortBy . comparing) fst [(3, 1), (2, 2), (1, 3)][(1,3),(2,2),(3,1)]
valueinsert :: Ord a => a -> [a] -> [a]
#

\mathcal{O}(n). The insert function takes an element and a list and inserts the element into the list at the first position where it is less than or equal to the next element. In particular, if the list is sorted before the call, the result will also be sorted. It is a special case of insertBy, which allows the programmer to supply their own comparison function.

Examples
Example1 expression
insert (-1) [1, 2, 3][-1,1,2,3]
Example1 expression
insert 'd' "abcefg""abcdefg"
Example1 expression
insert 4 [1, 2, 3, 5, 6, 7][1,2,3,4,5,6,7]

Zipping lists

8 declarations
valueunzip :: [(a, b)] %1 -> ([a], [b])
#
valueunzip3 :: [(a, b, c)] %1 -> ([a], [b], [c])
#

Orphan instances

3 instances
  • Monoid [a]
  • Semigroup (NonEmpty a)
  • Semigroup [a]