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

Modulererebase-1.21.2Haskell2010

GHC.List

  • 1 type
  • 60 values
datadata List a
#

The builtin linked list type.

In Haskell, lists are one of the most important data types as they are often used analogous to loops in imperative programming languages. These lists are singly linked, which makes them unsuited for operations that require \mathcal{O}(1) access. Instead, they are intended to be traversed.

You can use List a or [a] in type signatures:

length :: [a] -> Int

or

length :: List a -> Int

They are fully equivalent, and List a will be normalised to [a].

Usage

Lists are constructed recursively using the right-associative constructor operator (or cons) (:) :: a -> [a] -> [a], which prepends an element to a list, and the empty list [].

(1 : 2 : 3 : []) == (1 : (2 : (3 : []))) == [1, 2, 3]

Lists can also be constructed using list literals of the form [x_1, x_2, ..., x_n] which are syntactic sugar and, unless -XOverloadedLists is enabled, are translated into uses of (:) and []

String literals, like "I 💜 hs", are translated into Lists of characters, ['I', ' ', '💜', ' ', 'h', 's'].

Implementation

Internally and in memory, all the above are represented like this, with arrows being pointers to locations in memory.

╭───┬───┬──╮   ╭───┬───┬──╮   ╭───┬───┬──╮   ╭────╮
│(:)│   │ ─┼──>│(:)│   │ ─┼──>│(:)│   │ ─┼──>│ [] │
╰───┴─┼─┴──╯   ╰───┴─┼─┴──╯   ╰───┴─┼─┴──╯   ╰────╯
      v              v              v
      1              2              3
Examples
>>> ['H', 'a', 's', 'k', 'e', 'l', 'l']
"Haskell"
>>> 1 : [4, 1, 5, 9]
[1,4,1,5,9]
>>> [] : [] : []
[[],[]]
Instances73Monad, Functor, MonadFix, MonadFail, Applicative, Foldable, …
valueall :: (a -> Bool) -> [a] -> Bool
#

Applied to a predicate and a list, all determines if all elements of the list satisfy the predicate. For the result to be True, the list must be finite; False, however, results from a False value for the predicate applied to an element at a finite index of a finite or infinite list.

Examples
Example1 expression
all (> 3) []True
Example1 expression
all (> 3) [1,2]False
Example1 expression
all (> 3) [1,2,3,4,5]False
Example1 expression
all (> 3) [1..]False
Example1 expression
all (> 3) [4..]* Hangs forever *
valueand :: [Bool] -> Bool
#

and returns the conjunction of a Boolean list. For the result to be True, the list must be finite; False, however, results from a False value at a finite index of a finite or infinite list.

Examples
Example1 expression
and []True
Example1 expression
and [True]True
Example1 expression
and [False]False
Example1 expression
and [True, True, False]False
Example1 expression
and (False : repeat True) -- Infinite list [False,True,True,True,True,True,True...False
Example1 expression
and (repeat True)* Hangs forever *
valueany :: (a -> Bool) -> [a] -> Bool
#

Applied to a predicate and a list, any determines if any element of the list satisfies the predicate. For the result to be False, the list must be finite; True, however, results from a True value for the predicate applied to an element at a finite index of a finite or infinite list.

Examples
Example1 expression
any (> 3) []False
Example1 expression
any (> 3) [1,2]False
Example1 expression
any (> 3) [1,2,3,4,5]True
Example1 expression
any (> 3) [1..]True
Example1 expression
any (> 3) [0, -1..]* Hangs forever *
valueconcat :: [[a]] -> [a]
#

Concatenate a list of lists.

Examples
Example1 expression
concat [[1,2,3], [4,5], [6], []][1,2,3,4,5,6]
Example1 expression
concat [][]
Example1 expression
concat [[42]][42]
valueconcatMap :: (a -> [b]) -> [a] -> [b]
#

Map a function returning a list over a list and concatenate the results. concatMap can be seen as the composition of concat and map.

concatMap f xs == (concat . map f) xs
Examples
Example1 expression
concatMap (\i -> [-i,i]) [][]
Example1 expression
concatMap (\i -> [-i, i]) [1, 2, 3][-1,1,-2,2,-3,3]
Example1 expression
concatMap ('replicate' 3) [0, 2, 4][0,0,0,2,2,2,4,4,4]
valuenotElem :: Eq a => a -> [a] -> Bool
#

notElem is the negation of elem.

Examples
Example1 expression
3 `notElem` []True
Example1 expression
3 `notElem` [1,2]True
Example1 expression
3 `notElem` [1,2,3,4,5]False
Example1 expression
3 `notElem` [1..]False
Example1 expression
3 `notElem` [4..]* Hangs forever *
valueor :: [Bool] -> Bool
#

or returns the disjunction of a Boolean list. For the result to be False, the list must be finite; True, however, results from a True value at a finite index of a finite or infinite list.

Examples
Example1 expression
or []False
Example1 expression
or [True]True
Example1 expression
or [False]False
Example1 expression
or [True, True, False]True
Example1 expression
or (True : repeat False) -- Infinite list [True,False,False,False,False,False,False...True
Example1 expression
or (repeat False)* Hangs forever *
value(!!) :: HasCallStack => [a] -> Int -> a
#

List index (subscript) operator, starting from 0. It is an instance of the more general genericIndex, which takes an index of any integral type.

WARNING: This function is partial, and should only be used if you are sure that the indexing will not fail. Otherwise, use !?.

WARNING: This function takes linear time in the index.

Examples
Example1 expression
['a', 'b', 'c'] !! 0'a'
Example1 expression
['a', 'b', 'c'] !! 2'c'
Example1 expression
['a', 'b', 'c'] !! 3*** Exception: Prelude.!!: index too large
Example1 expression
['a', 'b', 'c'] !! (-1)*** Exception: Prelude.!!: negative index
value(!?) :: [a] -> Int -> Maybe a
#

List index (subscript) operator, starting from 0. Returns Nothing if the index is out of bounds

This is the total variant of the partial !! operator.

WARNING: This function takes linear time in the index.

Examples
Example1 expression
['a', 'b', 'c'] !? 0Just 'a'
Example1 expression
['a', 'b', 'c'] !? 2Just 'c'
Example1 expression
['a', 'b', 'c'] !? 3Nothing
Example1 expression
['a', 'b', 'c'] !? (-1)Nothing
valuebreak :: (a -> Bool) -> [a] -> ([a], [a])
#

break, 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 do not satisfy p and second element is the remainder of the list:

break p is equivalent to span (not . p) and consequently to (takeWhile (not . p) xs, dropWhile (not . p) xs), even if p is _|_.

Laziness
Example1 expression
break undefined []([],[])
Example1 expression
fst (break (const True) undefined)*** Exception: Prelude.undefined
Example1 expression
fst (break (const True) (undefined : undefined))[]
Example1 expression
take 1 (fst (break (const False) (1 : undefined)))[1]

break produces the first component of the tuple lazily:

Example1 expression
take 10 (fst (break (const False) [1..]))[1,2,3,4,5,6,7,8,9,10]
Examples
Example1 expression
break (> 3) [1,2,3,4,1,2,3,4]([1,2,3],[4,1,2,3,4])
Example1 expression
break (< 9) [1,2,3]([],[1,2,3])
Example1 expression
break (> 9) [1,2,3]([1,2,3],[])
valuecycle :: HasCallStack => [a] -> [a]
#

cycle ties a finite list into a circular one, or equivalently, the infinite repetition of the original list. It is the identity on infinite lists.

Examples
Example1 expression
cycle []*** Exception: Prelude.cycle: empty list
Example1 expression
take 10 (cycle [42])[42,42,42,42,42,42,42,42,42,42]
Example1 expression
take 10 (cycle [2, 5, 7])[2,5,7,2,5,7,2,5,7,2]
Example1 expression
take 1 (cycle (42 : undefined))[42]
valuedrop :: Int -> [a] -> [a]
#

drop n xs returns the suffix of xs after the first n elements, or [] if n >= length xs.

It is an instance of the more general genericDrop, in which n may be of any integral type.

Examples
Example1 expression
drop 6 "Hello World!""World!"
Example1 expression
drop 3 [1,2,3,4,5][4,5]
Example1 expression
drop 3 [1,2][]
Example1 expression
drop 3 [][]
Example1 expression
drop (-1) [1,2][1,2]
Example1 expression
drop 0 [1,2][1,2]
valuedropWhile :: (a -> Bool) -> [a] -> [a]
#

dropWhile p xs returns the suffix remaining after takeWhile p xs.

Examples
Example1 expression
dropWhile (< 3) [1,2,3,4,5,1,2,3][3,4,5,1,2,3]
Example1 expression
dropWhile (< 9) [1,2,3][]
Example1 expression
dropWhile (< 0) [1,2,3][1,2,3]
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
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
valueiterate :: (a -> a) -> a -> [a]
#

iterate f x returns an infinite list of repeated applications of f to x:

iterate f x == [x, f x, f (f x), ...]
Laziness

Note that iterate is lazy, potentially leading to thunk build-up if the consumer doesn't force each iterate. See iterate' for a strict variant of this function.

Example1 expression
take 1 $ iterate undefined 42[42]
Examples
Example1 expression
take 10 $ iterate not True[True,False,True,False,True,False,True,False,True,False]
Example1 expression
take 10 $ iterate (+3) 42[42,45,48,51,54,57,60,63,66,69]

iterate id == repeat:

Example1 expression
take 10 $ iterate id 1[1,1,1,1,1,1,1,1,1,1]
valueiterate' :: (a -> a) -> a -> [a]
#

iterate' is the strict version of iterate.

It forces the result of each application of the function to weak head normal form (WHNF) before proceeding.

Example1 expression
take 1 $ iterate' undefined 42*** Exception: Prelude.undefined
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
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"
valuerepeat :: a -> [a]
#

repeat x is an infinite list, with x the value of every element.

Examples
Example1 expression
take 10 $ repeat 17[17,17,17,17,17,17,17,17,17, 17]
Example1 expression
repeat undefined[*** Exception: Prelude.undefined
valuereplicate :: Int -> a -> [a]
#

replicate n x is a list of length n with x the value of every element. It is an instance of the more general genericReplicate, in which n may be of any integral type.

Examples
Example1 expression
replicate 0 True[]
Example1 expression
replicate (-1) True[]
Example1 expression
replicate 4 True[True,True,True,True]
valuereverse :: [a] -> [a]
#

\mathcal{O}(n). reverse xs returns the elements of xs in reverse order. xs must be finite.

Laziness

reverse is lazy in its elements.

Example1 expression
head (reverse [undefined, 1])1
Example1 expression
reverse (1 : 2 : undefined)*** Exception: Prelude.undefined
Examples
Example1 expression
reverse [][]
Example1 expression
reverse [42][42]
Example1 expression
reverse [2,5,7][7,5,2]
Example1 expression
reverse [1..]* Hangs forever *
valuescanl :: (b -> a -> b) -> b -> [a] -> [b]
#

\mathcal{O}(n). scanl is similar to foldl, but returns a list of successive reduced values from the left:

scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]

Note that

last (scanl f z xs) == foldl f z xs
Examples
Example1 expression
scanl (+) 0 [1..4][0,1,3,6,10]
Example1 expression
scanl (+) 42 [][42]
Example1 expression
scanl (-) 100 [1..4][100,99,97,94,90]
Example1 expression
scanl (\reversedString nextChar -> nextChar : reversedString) "foo" ['a', 'b', 'c', 'd']["foo","afoo","bafoo","cbafoo","dcbafoo"]
Example1 expression
take 10 (scanl (+) 0 [1..])[0,1,3,6,10,15,21,28,36,45]
Example1 expression
take 1 (scanl undefined 'a' undefined)"a"
valuescanl' :: (b -> a -> b) -> b -> [a] -> [b]
#

\mathcal{O}(n). A strict version of scanl.

valuescanl1 :: (a -> a -> a) -> [a] -> [a]
#

\mathcal{O}(n). scanl1 is a variant of scanl that has no starting value argument:

scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
Examples
Example1 expression
scanl1 (+) [1..4][1,3,6,10]
Example1 expression
scanl1 (+) [][]
Example1 expression
scanl1 (-) [1..4][1,-1,-4,-8]
Example1 expression
scanl1 (&&) [True, False, True, True][True,False,False,False]
Example1 expression
scanl1 (||) [False, False, True, True][False,False,True,True]
Example1 expression
take 10 (scanl1 (+) [1..])[1,3,6,10,15,21,28,36,45,55]
Example1 expression
take 1 (scanl1 undefined ('a' : undefined))"a"
valuescanr :: (a -> b -> b) -> b -> [a] -> [b]
#

\mathcal{O}(n). scanr is the right-to-left dual of scanl. Note that the order of parameters on the accumulating function are reversed compared to scanl. Also note that

head (scanr f z xs) == foldr f z xs.
Examples
Example1 expression
scanr (+) 0 [1..4][10,9,7,4,0]
Example1 expression
scanr (+) 42 [][42]
Example1 expression
scanr (-) 100 [1..4][98,-97,99,-96,100]
Example1 expression
scanr (\nextChar reversedString -> nextChar : reversedString) "foo" ['a', 'b', 'c', 'd']["abcdfoo","bcdfoo","cdfoo","dfoo","foo"]
Example1 expression
force $ scanr (+) 0 [1..]*** Exception: stack overflow
valuescanr1 :: (a -> a -> a) -> [a] -> [a]
#

\mathcal{O}(n). scanr1 is a variant of scanr that has no starting value argument.

Examples
Example1 expression
scanr1 (+) [1..4][10,9,7,4]
Example1 expression
scanr1 (+) [][]
Example1 expression
scanr1 (-) [1..4][-2,3,-1,4]
Example1 expression
scanr1 (&&) [True, False, True, True][False,False,True,True]
Example1 expression
scanr1 (||) [True, True, False, False][True,True,False,False]
Example1 expression
force $ scanr1 (+) [1..]*** Exception: stack overflow
valuespan :: (a -> Bool) -> [a] -> ([a], [a])
#

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

span p xs is equivalent to (takeWhile p xs, dropWhile p xs), even if p is _|_.

Laziness
Example4 expressions
span undefined []([],[])fst (span (const False) undefined)*** Exception: Prelude.undefinedfst (span (const False) (undefined : undefined))[]take 1 (fst (span (const True) (1 : undefined)))[1]

span produces the first component of the tuple lazily:

Example1 expression
take 10 (fst (span (const True) [1..]))[1,2,3,4,5,6,7,8,9,10]
Examples
Example1 expression
span (< 3) [1,2,3,4,1,2,3,4]([1,2],[3,4,1,2,3,4])
Example1 expression
span (< 9) [1,2,3]([1,2,3],[])
Example1 expression
span (< 0) [1,2,3]([],[1,2,3])
valuesplitAt :: Int -> [a] -> ([a], [a])
#

splitAt n xs returns a tuple where first element is xs prefix of length n and second element is the remainder of the list:

splitAt is an instance of the more general genericSplitAt, in which n may be of any integral type.

Laziness

It is equivalent to (take n xs, drop n xs) unless n is _|_: splitAt _|_ xs = _|_, not (_|_, _|_)).

The first component of the tuple is produced lazily:

Example1 expression
fst (splitAt 0 undefined)[]
Example1 expression
take 1 (fst (splitAt 10 (1 : undefined)))[1]
Examples
Example1 expression
splitAt 6 "Hello World!"("Hello ","World!")
Example1 expression
splitAt 3 [1,2,3,4,5]([1,2,3],[4,5])
Example1 expression
splitAt 1 [1,2,3]([1],[2,3])
Example1 expression
splitAt 3 [1,2,3]([1,2,3],[])
Example1 expression
splitAt 4 [1,2,3]([1,2,3],[])
Example1 expression
splitAt 0 [1,2,3]([],[1,2,3])
Example1 expression
splitAt (-1) [1,2,3]([],[1,2,3])
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
valuetake :: Int -> [a] -> [a]
#

take n, applied to a list xs, returns the prefix of xs of length n, or xs itself if n >= length xs.

It is an instance of the more general genericTake, in which n may be of any integral type.

Laziness
Example2 expressions
take 0 undefined[]take 2 (1 : 2 : undefined)[1,2]
Examples
Example1 expression
take 5 "Hello World!""Hello"
Example1 expression
take 3 [1,2,3,4,5][1,2,3]
Example1 expression
take 3 [1,2][1,2]
Example1 expression
take 3 [][]
Example1 expression
take (-1) [1,2][]
Example1 expression
take 0 [1,2][]
valuetakeWhile :: (a -> Bool) -> [a] -> [a]
#

takeWhile, applied to a predicate p and a list xs, returns the longest prefix (possibly empty) of xs of elements that satisfy p.

Laziness
Example1 expression
takeWhile (const False) undefined*** Exception: Prelude.undefined
Example1 expression
takeWhile (const False) (undefined : undefined)[]
Example1 expression
take 1 (takeWhile (const True) (1 : undefined))[1]
Examples
Example1 expression
takeWhile (< 3) [1,2,3,4,1,2,3,4][1,2]
Example1 expression
takeWhile (< 9) [1,2,3][1,2,3]
Example1 expression
takeWhile (< 0) [1,2,3][]
valueuncons :: [a] -> Maybe (a, [a])
#

\mathcal{O}(1). Decompose a list into its head and tail.

  • If the list is empty, returns Nothing.

  • If the list is non-empty, returns Just (x, xs), where x is the head of the list and xs its tail.

Examples
Example1 expression
uncons []Nothing
Example1 expression
uncons [1]Just (1,[])
Example1 expression
uncons [1, 2, 3]Just (1,[2,3])
valueunsnoc :: [a] -> Maybe ([a], a)
#

\mathcal{O}(n). Decompose a list into init and last.

  • If the list is empty, returns Nothing.

  • If the list is non-empty, returns Just (xs, x), where xs is the initial part of the list and x is its last element.

unsnoc is dual to uncons: for a finite list xs

unsnoc xs = (\(hd, tl) -> (reverse tl, hd)) <$> uncons (reverse xs)
Examples
Example1 expression
unsnoc []Nothing
Example1 expression
unsnoc [1]Just ([],1)
Example1 expression
unsnoc [1, 2, 3]Just ([1,2],3)
Laziness
Example1 expression
fst <$> unsnoc [undefined]Just []
Example1 expression
head . fst <$> unsnoc (1 : undefined)Just *** Exception: Prelude.undefined
Example1 expression
head . fst <$> unsnoc (1 : 2 : undefined)Just 1
valueunzip :: [(a, b)] -> ([a], [b])
#

unzip transforms a list of pairs into a list of first components and a list of second components.

Examples
Example1 expression
unzip []([],[])
Example1 expression
unzip [(1, 'a'), (2, 'b')]([1,2],"ab")
valueunzip3 :: [(a, b, c)] -> ([a], [b], [c])
#

The unzip3 function takes a list of triples and returns three lists of the respective components, analogous to unzip.

Examples
Example1 expression
unzip3 []([],[],[])
Example1 expression
unzip3 [(1, 'a', True), (2, 'b', False)]([1,2],"ab",[True,False])
valuezip3 :: [a] -> [b] -> [c] -> [(a, b, c)]
#

zip3 takes three lists and returns a list of triples, analogous to zip. It is capable of list fusion, but it is restricted to its first list argument and its resulting list.

valuezipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
#

\mathcal{O}(\min(m,n)). zipWith generalises zip by zipping with the function given as the first argument, instead of a tupling function.

zipWith (,) xs ys == zip xs ys
zipWith f [x1,x2,x3..] [y1,y2,y3..] == [f x1 y1, f x2 y2, f x3 y3..]

zipWith is right-lazy:

Example2 expressions
let f = undefinedzipWith f [] undefined[]

zipWith is capable of list fusion, but it is restricted to its first list argument and its resulting list.

Examples

zipWith (+) can be applied to two lists to produce the list of corresponding sums:

Example1 expression
zipWith (+) [1, 2, 3] [4, 5, 6][5,7,9]
Example1 expression
zipWith (++) ["hello ", "foo"] ["world!", "bar"]["hello world!","foobar"]
valuezipWith3 :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
#

\mathcal{O}(\min(l,m,n)). The zipWith3 function takes a function which combines three elements, as well as three lists and returns a list of the function applied to corresponding elements, analogous to zipWith. It is capable of list fusion, but it is restricted to its first list argument and its resulting list.

zipWith3 (,,) xs ys zs == zip3 xs ys zs
zipWith3 f [x1,x2,x3..] [y1,y2,y3..] [z1,z2,z3..] == [f x1 y1 z1, f x2 y2 z2, f x3 y3 z3..]
Examples
Example1 expression
zipWith3 (\x y z -> [x, y, z]) "123" "abc" "xyz"["1ax","2by","3cz"]
Example1 expression
zipWith3 (\x y z -> (x * y) + z) [1, 2, 3] [4, 5, 6] [7, 8, 9][11,18,27]
valueelem :: Eq a => a -> [a] -> Bool
#

elem is the list membership predicate, usually written in infix form, e.g., x `elem` xs. For the result to be False, the list must be finite; True, however, results from an element equal to x found at a finite index of a finite or infinite list.

Examples
Example1 expression
3 `elem` []False
Example1 expression
3 `elem` [1,2]False
Example1 expression
3 `elem` [1,2,3,4,5]True
Example1 expression
3 `elem` [1..]True
Example1 expression
3 `elem` [4..]* Hangs forever *
valuefoldl :: (b -> a -> b) -> b -> [a] -> b
#

foldl, applied to a binary operator, a starting value (typically the left-identity of the operator), and a list, reduces the list using the binary operator, from left to right:

foldl f z [x1, x2, ..., xn] == (...((z `f` x1) `f` x2) `f`...) `f` xn

The list must be finite.

Example5 expressions
foldl (+) 0 [1..4]10foldl (+) 42 []42foldl (-) 100 [1..4]90foldl (\reversedString nextChar -> nextChar : reversedString) "foo" ['a', 'b', 'c', 'd']"dcbafoo"foldl (+) 0 [1..]* Hangs forever *
valuefoldl' :: (b -> a -> b) -> b -> [a] -> b
#

A strict version of foldl.

valuefoldl1 :: HasCallStack => (a -> a -> a) -> [a] -> a
#

foldl1 is a variant of foldl that has no starting value argument, and thus must be applied to non-empty lists. Note that unlike foldl, the accumulated value must be of the same type as the list elements.

Example6 expressions
foldl1 (+) [1..4]10foldl1 (+) []*** Exception: Prelude.foldl1: empty listfoldl1 (-) [1..4]-8foldl1 (&&) [True, False, True, True]Falsefoldl1 (||) [False, False, True, True]Truefoldl1 (+) [1..]* Hangs forever *
valuefoldr :: (a -> b -> b) -> b -> [a] -> b
#

foldr, applied to a binary operator, a starting value (typically the right-identity of the operator), and a list, reduces the list using the binary operator, from right to left:

foldr f z [x1, x2, ..., xn] == x1 `f` (x2 `f` ... (xn `f` z)...)
valuefoldr' :: (a -> b -> b) -> b -> [a] -> b
#

foldr' is a variant of foldr that begins list reduction from the last element and evaluates the accumulator strictly as it unwinds the stack back to the beginning of the list. The input list must be finite, otherwise foldr' runs out of space (diverges).

Note that if the function that combines the accumulated value with each element is strict in the accumulator, other than a possible improvement in the constant factor, you get the same \mathcal{O}(n) space cost as with just foldr.

If you want a strict right fold in constant space, you need a structure that supports faster than \mathcal{O}(n) access to the right-most element, such as Seq from the containers package.

Use of this function is a hint that the [] structure may be a poor fit for the task at hand. If the order in which the elements are combined is not important, use foldl' instead.

Example3 expressions
foldr' (+) [1..4]  -- Use foldl' instead!10foldr' (&&) [True, False, True, True] -- Use foldr instead!Falsefoldr' (||) [False, False, True, True] -- Use foldr instead!True
valuefoldr1 :: HasCallStack => (a -> a -> a) -> [a] -> a
#

foldr1 is a variant of foldr that has no starting value argument, and thus must be applied to non-empty lists. Note that unlike foldr, the accumulated value must be of the same type as the list elements.

Example6 expressions
foldr1 (+) [1..4]10foldr1 (+) []*** Exception: Prelude.foldr1: empty listfoldr1 (-) [1..4]-2foldr1 (&&) [True, False, True, True]Falsefoldr1 (||) [False, False, True, True]Trueforce $ foldr1 (+) [1..]*** Exception: stack overflow
valuelength :: [a] -> Int
#

\mathcal{O}(n). length returns the length of a finite list as an Int. It is an instance of the more general genericLength, the result type of which may be any kind of number.

Example3 expressions
length []0length ['a', 'b', 'c']3length [1..]* Hangs forever *
valuemaximum :: (Ord a, HasCallStack) => [a] -> a
#

maximum returns the maximum value from a list, which must be non-empty, finite, and of an ordered type. It is a special case of GHC.Internal.Data.List.maximumBy, which allows the programmer to supply their own comparison function.

Example4 expressions
maximum []*** Exception: Prelude.maximum: empty listmaximum [42]42maximum [55, -12, 7, 0, -89]55maximum [1..]* Hangs forever *
valueminimum :: (Ord a, HasCallStack) => [a] -> a
#

minimum returns the minimum value from a list, which must be non-empty, finite, and of an ordered type. It is a special case of GHC.Internal.Data.List.minimumBy, which allows the programmer to supply their own comparison function.

Example4 expressions
minimum []*** Exception: Prelude.minimum: empty listminimum [42]42minimum [55, -12, 7, 0, -89]-89minimum [1..]* Hangs forever *
valuenull :: [a] -> Bool
#

\mathcal{O}(1). Test whether a list is empty.

Example3 expressions
null []Truenull [1]Falsenull [1..]False
valueproduct :: Num a => [a] -> a
#

The product function computes the product of a finite list of numbers.

Example5 expressions
product []1product [42]42product [1..10]3628800product [4.1, 2.0, 1.7]13.939999999999998product [1..]* Hangs forever *
valuesum :: Num a => [a] -> a
#

The sum function computes the sum of a finite list of numbers.

Example5 expressions
sum []0sum [42]42sum [1..10]55sum [4.1, 2.0, 1.7]7.8sum [1..]* Hangs forever *
value(++) :: [a] -> [a] -> [a]
#

(++) appends two lists, i.e.,

[x1, ..., xm] ++ [y1, ..., yn] == [x1, ..., xm, y1, ..., yn]
[x1, ..., xm] ++ [y1, ...] == [x1, ..., xm, y1, ...]

If the first list is not finite, the result is the first list.

Performance considerations

This function takes linear time in the number of elements of the first list. Thus it is better to associate repeated applications of (++) to the right (which is the default behaviour): xs ++ (ys ++ zs) or simply xs ++ ys ++ zs, but not (xs ++ ys) ++ zs. For the same reason GHC.Internal.Data.List.concat = GHC.Internal.Data.List.foldr (++) [] has linear performance, while GHC.Internal.Data.List.foldl (++) [] is prone to quadratic slowdown

Examples
Example1 expression
[1, 2, 3] ++ [4, 5, 6][1,2,3,4,5,6]
Example1 expression
[] ++ [1, 2, 3][1,2,3]
Example1 expression
[3, 2, 1] ++ [][3,2,1]
valueaugment :: (forall b. (a -> b -> b) -> b -> b) -> [a] -> [a]
#

A list producer that can be fused with foldr. This function is merely

   augment g xs = g (:) xs

but GHC's simplifier will transform an expression of the form foldr k z (augment g xs), which may arise after inlining, to g k (foldr k z xs), which avoids producing an intermediate list.

valuebuild :: (forall b. (a -> b -> b) -> b -> b) -> [a]
#

A list producer that can be fused with foldr. This function is merely

   build g = g (:) []

but GHC's simplifier will transform an expression of the form foldr k z (build g), which may arise after inlining, to g k z, which avoids producing an intermediate list.

valuemap :: (a -> b) -> [a] -> [b]
#

\mathcal{O}(n). map f xs is the list obtained by applying f to each element of xs, i.e.,

map f [x1, x2, ..., xn] == [f x1, f x2, ..., f xn]
map f [x1, x2, ...] == [f x1, f x2, ...]

this means that map id == id

Examples
Example1 expression
map (+1) [1, 2, 3][2,3,4]
Example1 expression
map id [1, 2, 3][1,2,3]
Example1 expression
map (\n -> 3 * n + 1) [1, 2, 3][4,7,10]
valuezip :: [a] -> [b] -> [(a, b)]
#

\mathcal{O}(\min(m,n)). zip takes two lists and returns a list of corresponding pairs.

zip is right-lazy:

Example2 expressions
zip [] undefined[]zip undefined []*** Exception: Prelude.undefined...

zip is capable of list fusion, but it is restricted to its first list argument and its resulting list.

Examples
Example1 expression
zip [1, 2, 3] ['a', 'b', 'c'][(1,'a'),(2,'b'),(3,'c')]

If one input list is shorter than the other, excess elements of the longer list are discarded, even if one of the lists is infinite:

Example1 expression
zip [1] ['a', 'b'][(1,'a')]
Example1 expression
zip [1, 2] ['a'][(1,'a')]
Example1 expression
zip [] [1..][]
Example1 expression
zip [1..] [][]
valuefilter :: (a -> Bool) -> [a] -> [a]
#

\mathcal{O}(n). filter, applied to a predicate and a list, returns the list of those elements that satisfy the predicate; i.e.,

filter p xs = [ x | x <- xs, p x]
Examples
Example1 expression
filter odd [1, 2, 3][1,3]
Example1 expression
filter (\l -> length l > 3) ["Hello", ", ", "World", "!"]["Hello","World"]
Example1 expression
filter (/= 3) [1, 2, 3, 4, 3, 2, 1][1,2,4,2,1]