Data.List (original) (raw)
(++) :: [a] -> [a] -> [a] infixr 5 Source #
Append 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.
\(\mathcal{O}(1)\). Extract the first element of a list, which must be non-empty.
\(\mathcal{O}(n)\). Extract the last element of a list, which must be finite and non-empty.
\(\mathcal{O}(1)\). Extract the elements after the head of a list, which must be non-empty.
\(\mathcal{O}(n)\). Return all the elements of a list except the last one. The list must be non-empty.
uncons :: [a] -> Maybe (a, [a]) Source #
\(\mathcal{O}(1)\). Decompose a list into its head and tail. If the list is empty, returns [Nothing](Data-Maybe.html#v:Nothing "Data.Maybe")
. If the list is non-empty, returns `[Just](Data-Maybe.html#v:Just "Data.Maybe")` (x, xs)
, where x
is the head of the list and xs
its tail.
Since: 4.8.0.0
null :: Foldable t => t a -> Bool Source #
Test whether the structure is empty. The default implementation is optimized for structures that are similar to cons-lists, because there is no general way to do better.
Since: 4.8.0.0
length :: Foldable t => t a -> Int Source #
Returns the size/length of a finite structure as an [Int](Data-Int.html#t:Int "Data.Int")
. The default implementation is optimized for structures that are similar to cons-lists, because there is no general way to do better.
Since: 4.8.0.0
map :: (a -> b) -> [a] -> [b] Source #
\(\mathcal{O}(n)\). [map](Data-List.html#v:map "Data.List")
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, ...]
>>>
map (+1) [1, 2, 3]** **
reverse :: [a] -> [a] Source #
[reverse](Data-List.html#v:reverse "Data.List")
xs
returns the elements of xs
in reverse order.xs
must be finite.
intersperse :: a -> [a] -> [a] Source #
\(\mathcal{O}(n)\). The [intersperse](Data-List.html#v:intersperse "Data.List")
function takes an element and a list and `intersperses' that element between the elements of the list. For example,
>>>
intersperse ',' "abcde"** **
"a,b,c,d,e"
intercalate :: [a] -> [[a]] -> [a] Source #
[intercalate](Data-List.html#v:intercalate "Data.List")
xs xss
is equivalent to (`[concat](GHC-List.html#v:concat "GHC.List")` (`[intersperse](Data-List.html#v:intersperse "Data.List")` xs xss))
. It inserts the list xs
in between the lists in xss
and concatenates the result.
>>>
intercalate ", " ["Lorem", "ipsum", "dolor"]** **
"Lorem, ipsum, dolor"
transpose :: [[a]] -> [[a]] Source #
The [transpose](Data-List.html#v:transpose "Data.List")
function transposes the rows and columns of its argument. For example,
>>>
transpose [[1,2,3],[4,5,6]]** **
[[1,4],[2,5],[3,6]]
If some of the rows are shorter than the following rows, their elements are skipped:
>>>
transpose [[10,11],[20],[],[30,31,32]]** **
[[10,20,30],[11,31],[32]]
subsequences :: [a] -> [[a]] Source #
The [subsequences](Data-List.html#v:subsequences "Data.List")
function returns the list of all subsequences of the argument.
>>>
subsequences "abc"** **
["","a","b","ab","c","ac","bc","abc"]
permutations :: [a] -> [[a]] Source #
The [permutations](Data-List.html#v:permutations "Data.List")
function returns the list of all permutations of the argument.
>>>
permutations "abc"** **
["abc","bac","cba","bca","cab","acb"]
foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b Source #
Left-associative fold of a structure.
In the case of lists, [foldl](Data-List.html#v:foldl "Data.List")
, when 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
Note that to produce the outermost application of the operator the entire input list must be traversed. This means that [foldl'](Data-List.html#v:foldl-39- "Data.List")
will diverge if given an infinite list.
Also note that if you want an efficient left-fold, you probably want to use [foldl'](Data-List.html#v:foldl-39- "Data.List")
instead of [foldl](Data-List.html#v:foldl "Data.List")
. The reason for this is that latter does not force the "inner" results (e.g. z `f` x1
in the above example) before applying them to the operator (e.g. to (`f` x2)
). This results in a thunk chain \(\mathcal{O}(n)\) elements long, which then must be evaluated from the outside-in.
For a general [Foldable](Data-Foldable.html#t:Foldable "Data.Foldable")
structure this should be semantically identical to,
foldl f z = [foldl](GHC-List.html#v:foldl "GHC.List")
f z . [toList](Data-Foldable.html#v:toList "Data.Foldable")
foldl' :: Foldable t => (b -> a -> b) -> b -> t a -> b Source #
Left-associative fold of a structure but with strict application of the operator.
This ensures that each step of the fold is forced to weak head normal form before being applied, avoiding the collection of thunks that would otherwise occur. This is often what you want to strictly reduce a finite list to a single, monolithic result (e.g. [length](Data-List.html#v:length "Data.List")
).
For a general [Foldable](Data-Foldable.html#t:Foldable "Data.Foldable")
structure this should be semantically identical to,
foldl' f z = [foldl'](GHC-List.html#v:foldl-39- "GHC.List")
f z . [toList](Data-Foldable.html#v:toList "Data.Foldable")
Since: 4.6.0.0
foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b Source #
Right-associative fold of a structure.
In the case of lists, [foldr](Data-List.html#v:foldr "Data.List")
, when 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)...)
Note that, since the head of the resulting expression is produced by an application of the operator to the first element of the list,[foldr](Data-List.html#v:foldr "Data.List")
can produce a terminating expression from an infinite list.
For a general [Foldable](Data-Foldable.html#t:Foldable "Data.Foldable")
structure this should be semantically identical to,
foldr f z = [foldr](GHC-List.html#v:foldr "GHC.List")
f z . [toList](Data-Foldable.html#v:toList "Data.Foldable")
concat :: Foldable t => t [a] -> [a] Source #
The concatenation of all the elements of a container of lists.
concatMap :: Foldable t => (a -> [b]) -> t a -> [b] Source #
Map a function over all the elements of a container and concatenate the resulting lists.
and :: Foldable t => t Bool -> Bool Source #
[and](Data-List.html#v:and "Data.List")
returns the conjunction of a container of Bools. For the result to be [True](Data-Bool.html#v:True "Data.Bool")
, the container must be finite; [False](Data-Bool.html#v:False "Data.Bool")
, however, results from a [False](Data-Bool.html#v:False "Data.Bool")
value finitely far from the left end.
or :: Foldable t => t Bool -> Bool Source #
[or](Data-List.html#v:or "Data.List")
returns the disjunction of a container of Bools. For the result to be [False](Data-Bool.html#v:False "Data.Bool")
, the container must be finite; [True](Data-Bool.html#v:True "Data.Bool")
, however, results from a [True](Data-Bool.html#v:True "Data.Bool")
value finitely far from the left end.
any :: Foldable t => (a -> Bool) -> t a -> Bool Source #
Determines whether any element of the structure satisfies the predicate.
all :: Foldable t => (a -> Bool) -> t a -> Bool Source #
Determines whether all elements of the structure satisfy the predicate.
sum :: (Foldable t, Num a) => t a -> a Source #
The [sum](Data-List.html#v:sum "Data.List")
function computes the sum of the numbers of a structure.
Since: 4.8.0.0
scanl :: (b -> a -> b) -> b -> [a] -> [b] Source #
\(\mathcal{O}(n)\). [scanl](Data-List.html#v:scanl "Data.List")
is similar to [foldl](GHC-List.html#v:foldl "GHC.List")
, 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.
scanl' :: (b -> a -> b) -> b -> [a] -> [b] Source #
\(\mathcal{O}(n)\). A strictly accumulating version of [scanl](Data-List.html#v:scanl "Data.List")
scanl1 :: (a -> a -> a) -> [a] -> [a] Source #
\(\mathcal{O}(n)\). [scanl1](Data-List.html#v:scanl1 "Data.List")
is a variant of [scanl](Data-List.html#v:scanl "Data.List")
that has no starting value argument:
scanl1 f [x1, x2, ...] == [x1, x1 f
x2, ...]
scanr :: (a -> b -> b) -> b -> [a] -> [b] Source #
\(\mathcal{O}(n)\). [scanr](Data-List.html#v:scanr "Data.List")
is the right-to-left dual of [scanl](Data-List.html#v:scanl "Data.List")
. Note that
head (scanr f z xs) == foldr f z xs.
scanr1 :: (a -> a -> a) -> [a] -> [a] Source #
\(\mathcal{O}(n)\). [scanr1](Data-List.html#v:scanr1 "Data.List")
is a variant of [scanr](Data-List.html#v:scanr "Data.List")
that has no starting value argument.
mapAccumL :: Traversable t => (a -> b -> (a, c)) -> a -> t b -> (a, t c) Source #
The [mapAccumL](Data-List.html#v:mapAccumL "Data.List")
function behaves like a combination of [fmap](Data-Functor.html#v:fmap "Data.Functor")
and [foldl](Data-Foldable.html#v:foldl "Data.Foldable")
; it applies a function to each element of a structure, passing an accumulating parameter from left to right, and returning a final value of this accumulator together with the new structure.
mapAccumR :: Traversable t => (a -> b -> (a, c)) -> a -> t b -> (a, t c) Source #
The [mapAccumR](Data-List.html#v:mapAccumR "Data.List")
function behaves like a combination of [fmap](Data-Functor.html#v:fmap "Data.Functor")
and [foldr](Data-Foldable.html#v:foldr "Data.Foldable")
; it applies a function to each element of a structure, passing an accumulating parameter from right to left, and returning a final value of this accumulator together with the new structure.
iterate :: (a -> a) -> a -> [a] Source #
[iterate](Data-List.html#v:iterate "Data.List")
f x
returns an infinite list of repeated applications of f
to x
:
iterate f x == [x, f x, f (f x), ...]
Note that [iterate](Data-List.html#v:iterate "Data.List")
is lazy, potentially leading to thunk build-up if the consumer doesn't force each iterate. See [iterate'](Data-List.html#v:iterate-39- "Data.List")
for a strict variant of this function.
iterate' :: (a -> a) -> a -> [a] Source #
[iterate'](Data-List.html#v:iterate-39- "Data.List")
is the strict version of [iterate](Data-List.html#v:iterate "Data.List")
.
It ensures that the result of each application of force to weak head normal form before proceeding.
[cycle](Data-List.html#v:cycle "Data.List")
ties a finite list into a circular one, or equivalently, the infinite repetition of the original list. It is the identity on infinite lists.
unfoldr :: (b -> Maybe (a, b)) -> b -> [a] Source #
The [unfoldr](Data-List.html#v:unfoldr "Data.List")
function is a `dual' to [foldr](GHC-List.html#v:foldr "GHC.List")
: while [foldr](GHC-List.html#v:foldr "GHC.List")
reduces a list to a summary value, [unfoldr](Data-List.html#v:unfoldr "Data.List")
builds a list from a seed value. The function takes the element and returns [Nothing](Data-Maybe.html#v:Nothing "Data.Maybe")
if it is done producing the list or returns [Just](Data-Maybe.html#v:Just "Data.Maybe")
(a,b)
, in which case, a
is a prepended to the list and b
is used as the next element in a recursive call. For example,
iterate f == unfoldr (\x -> Just (x, f x))
In some cases, [unfoldr](Data-List.html#v:unfoldr "Data.List")
can undo a [foldr](GHC-List.html#v:foldr "GHC.List")
operation:
unfoldr f' (foldr f z xs) == xs
if the following holds:
f' (f x y) = Just (x,y) f' z = Nothing
A simple use of unfoldr:
>>>
unfoldr (\b -> if b == 0 then Nothing else Just (b, b-1)) 10** **
[10,9,8,7,6,5,4,3,2,1]
take :: Int -> [a] -> [a] Source #
[take](Data-List.html#v:take "Data.List")
n
, applied to a list xs
, returns the prefix of xs
of length n
, or xs
itself if n > `[length](GHC-List.html#v:length "GHC.List")` xs
:
take 5 "Hello World!" == "Hello" take 3 [1,2,3,4,5] == [1,2,3] take 3 [1,2] == [1,2] take 3 [] == [] take (-1) [1,2] == [] take 0 [1,2] == []
It is an instance of the more general [genericTake](Data-List.html#v:genericTake "Data.List")
, in which n
may be of any integral type.
drop :: Int -> [a] -> [a] Source #
[drop](Data-List.html#v:drop "Data.List")
n xs
returns the suffix of xs
after the first n
elements, or []
if n > `[length](GHC-List.html#v:length "GHC.List")` xs
:
drop 6 "Hello World!" == "World!" drop 3 [1,2,3,4,5] == [4,5] drop 3 [1,2] == [] drop 3 [] == [] drop (-1) [1,2] == [1,2] drop 0 [1,2] == [1,2]
It is an instance of the more general [genericDrop](Data-List.html#v:genericDrop "Data.List")
, in which n
may be of any integral type.
splitAt :: Int -> [a] -> ([a], [a]) Source #
[splitAt](Data-List.html#v:splitAt "Data.List")
n xs
returns a tuple where first element is xs
prefix of length n
and second element is the remainder of the list:
splitAt 6 "Hello World!" == ("Hello ","World!") splitAt 3 [1,2,3,4,5] == ([1,2,3],[4,5]) splitAt 1 [1,2,3] == ([1],[2,3]) splitAt 3 [1,2,3] == ([1,2,3],[]) splitAt 4 [1,2,3] == ([1,2,3],[]) splitAt 0 [1,2,3] == ([],[1,2,3]) splitAt (-1) [1,2,3] == ([],[1,2,3])
It is equivalent to (`[take](Data-List.html#v:take "Data.List")` n xs, `[drop](Data-List.html#v:drop "Data.List")` n xs)
when n
is not _|_
(splitAt _|_ xs = _|_
).[splitAt](Data-List.html#v:splitAt "Data.List")
is an instance of the more general [genericSplitAt](Data-List.html#v:genericSplitAt "Data.List")
, in which n
may be of any integral type.
takeWhile :: (a -> Bool) -> [a] -> [a] Source #
[takeWhile](Data-List.html#v:takeWhile "Data.List")
, applied to a predicate p
and a list xs
, returns the longest prefix (possibly empty) of xs
of elements that satisfy p
:
takeWhile (< 3) [1,2,3,4,1,2,3,4] == [1,2] takeWhile (< 9) [1,2,3] == [1,2,3] takeWhile (< 0) [1,2,3] == []
dropWhile :: (a -> Bool) -> [a] -> [a] Source #
[dropWhile](Data-List.html#v:dropWhile "Data.List")
p xs
returns the suffix remaining after [takeWhile](Data-List.html#v:takeWhile "Data.List")
p xs
:
dropWhile (< 3) [1,2,3,4,5,1,2,3] == [3,4,5,1,2,3] dropWhile (< 9) [1,2,3] == [] dropWhile (< 0) [1,2,3] == [1,2,3]
dropWhileEnd :: (a -> Bool) -> [a] -> [a] Source #
The [dropWhileEnd](Data-List.html#v:dropWhileEnd "Data.List")
function drops the largest suffix of a list in which the given predicate holds for all elements. For example:
>>>
dropWhileEnd isSpace "foo\n"** **
"foo"
>>>
dropWhileEnd isSpace "foo bar"** **
"foo bar"
dropWhileEnd isSpace ("foo\n" ++ undefined) == "foo" ++ undefined
Since: 4.5.0.0
span :: (a -> Bool) -> [a] -> ([a], [a]) Source #
[span](Data-List.html#v:span "Data.List")
, 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:
span (< 3) [1,2,3,4,1,2,3,4] == ([1,2],[3,4,1,2,3,4]) span (< 9) [1,2,3] == ([1,2,3],[]) span (< 0) [1,2,3] == ([],[1,2,3])
[span](Data-List.html#v:span "Data.List")
p xs
is equivalent to (`[takeWhile](Data-List.html#v:takeWhile "Data.List")` p xs, `[dropWhile](Data-List.html#v:dropWhile "Data.List")` p xs)
break :: (a -> Bool) -> [a] -> ([a], [a]) Source #
[break](Data-List.html#v:break "Data.List")
, 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 (> 3) [1,2,3,4,1,2,3,4] == ([1,2,3],[4,1,2,3,4]) break (< 9) [1,2,3] == ([],[1,2,3]) break (> 9) [1,2,3] == ([1,2,3],[])
[break](Data-List.html#v:break "Data.List")
p
is equivalent to `[span](Data-List.html#v:span "Data.List")` (`[not](Data-Bool.html#v:not "Data.Bool")` . p)
.
stripPrefix :: Eq a => [a] -> [a] -> Maybe [a] Source #
\(\mathcal{O}(\min(m,n))\). The [stripPrefix](Data-List.html#v:stripPrefix "Data.List")
function drops the given prefix from a list. It returns [Nothing](Data-Maybe.html#v:Nothing "Data.Maybe")
if the list did not start with the prefix given, or [Just](Data-Maybe.html#v:Just "Data.Maybe")
the list after the prefix, if it does.
>>>
**stripPrefix "foo" "foobar"** **
**Just "bar"
>>>
**stripPrefix "foo" "foo"** **
**Just ""
>>>
**stripPrefix "foo" "barfoo"** **
**Nothing
>>>
**stripPrefix "foo" "barfoobaz"** **
**Nothing
group :: Eq a => [a] -> [[a]] Source #
The [group](Data-List.html#v:group "Data.List")
function takes a list and returns a list of lists such that the concatenation of the result is equal to the argument. Moreover, each sublist in the result contains only equal elements. For example,
>>>
group "Mississippi"** **
["M","i","ss","i","ss","i","pp","i"]
It is a special case of [groupBy](Data-List.html#v:groupBy "Data.List")
, which allows the programmer to supply their own equality test.
inits :: [a] -> [[a]] Source #
The [inits](Data-List.html#v:inits "Data.List")
function returns all initial segments of the argument, shortest first. For example,
>>>
inits "abc"** **
["","a","ab","abc"]
Note that [inits](Data-List.html#v:inits "Data.List")
has the following strictness property:inits (xs ++ _|_) = inits xs ++ _|_
In particular,inits _|_ = [] : _|_
tails :: [a] -> [[a]] Source #
\(\mathcal{O}(n)\). The [tails](Data-List.html#v:tails "Data.List")
function returns all final segments of the argument, longest first. For example,
>>>
tails "abc"** **
["abc","bc","c",""]
Note that [tails](Data-List.html#v:tails "Data.List")
has the following strictness property:tails _|_ = _|_ : _|_
isPrefixOf :: Eq a => [a] -> [a] -> Bool Source #
\(\mathcal{O}(\min(m,n))\). The [isPrefixOf](Data-List.html#v:isPrefixOf "Data.List")
function takes two lists and returns [True](Data-Bool.html#v:True "Data.Bool")
iff the first list is a prefix of the second.
>>>
`` "Hello" isPrefixOf
"Hello World!"
**``**True
>>>
`` "Hello" isPrefixOf
"Wello Horld!"
**``**False
isSuffixOf :: Eq a => [a] -> [a] -> Bool Source #
The [isSuffixOf](Data-List.html#v:isSuffixOf "Data.List")
function takes two lists and returns [True](Data-Bool.html#v:True "Data.Bool")
iff the first list is a suffix of the second. The second list must be finite.
>>>
`` "ld!" isSuffixOf
"Hello World!"
**``**True
>>>
`` "World" isSuffixOf
"Hello World!"
**``**False
isInfixOf :: Eq a => [a] -> [a] -> Bool Source #
The [isInfixOf](Data-List.html#v:isInfixOf "Data.List")
function takes two lists and returns [True](Data-Bool.html#v:True "Data.Bool")
iff the first list is contained, wholly and intact, anywhere within the second.
>>>
**isInfixOf "Haskell" "I really like Haskell."** **
**True
>>>
**isInfixOf "Ial" "I really like Haskell."** **
**False
isSubsequenceOf :: Eq a => [a] -> [a] -> Bool Source #
The [isSubsequenceOf](Data-List.html#v:isSubsequenceOf "Data.List")
function takes two lists and returns [True](Data-Bool.html#v:True "Data.Bool")
if all the elements of the first list occur, in order, in the second. The elements do not have to occur consecutively.
`[isSubsequenceOf](Data-List.html#v:isSubsequenceOf "Data.List")` x y
is equivalent to `[elem](Data-List.html#v:elem "Data.List")` x (`[subsequences](Data-List.html#v:subsequences "Data.List")` y)
.
Examples
Expand
>>>
**isSubsequenceOf "GHC" "The Glorious Haskell Compiler"** **
**True
>>>
**isSubsequenceOf ['a','d'..'z'] ['a'..'z']** **
**True
>>>
**isSubsequenceOf [1..10] [10,9..0]** **
**False
Since: 4.8.0.0
Searching listsSearching by equality
lookup :: Eq a => a -> [(a, b)] -> Maybe b Source #
\(\mathcal{O}(n)\). [lookup](Data-List.html#v:lookup "Data.List")
key assocs
looks up a key in an association list.
>>>
**lookup 2 [(1, "first"), (2, "second"), (3, "third")]** **
**Just "second"
find :: Foldable t => (a -> Bool) -> t a -> Maybe a Source #
The [find](Data-List.html#v:find "Data.List")
function takes a predicate and a structure and returns the leftmost element of the structure matching the predicate, or[Nothing](Data-Maybe.html#v:Nothing "Data.Maybe")
if there is no such element.
filter :: (a -> Bool) -> [a] -> [a] Source #
\(\mathcal{O}(n)\). [filter](Data-List.html#v:filter "Data.List")
, 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]
>>>
filter odd [1, 2, 3]** **
[1,3]
partition :: (a -> Bool) -> [a] -> ([a], [a]) Source #
The [partition](Data-List.html#v:partition "Data.List")
function takes a predicate a list and returns the pair of lists of elements which do and do not satisfy the predicate, respectively; i.e.,
partition p xs == (filter p xs, filter (not . p) xs)
>>>
`` partition (elem
"aeiou") "Hello World!"
``("eoo","Hll Wrld!")
These functions treat a list xs
as a indexed collection, with indices ranging from 0 to `[length](Data-List.html#v:length "Data.List")` xs - 1
.
(!!) :: [a] -> Int -> a infixl 9 Source #
List index (subscript) operator, starting from 0. It is an instance of the more general [genericIndex](Data-List.html#v:genericIndex "Data.List")
, which takes an index of any integral type.
elemIndex :: Eq a => a -> [a] -> Maybe Int Source #
The [elemIndex](Data-List.html#v:elemIndex "Data.List")
function returns the index of the first element in the given list which is equal (by [==](Data-Eq.html#v:-61--61- "Data.Eq")
) to the query element, or [Nothing](Data-Maybe.html#v:Nothing "Data.Maybe")
if there is no such element.
>>>
**elemIndex 4 [0..]** **
**Just 4
elemIndices :: Eq a => a -> [a] -> [Int] Source #
The [elemIndices](Data-List.html#v:elemIndices "Data.List")
function extends [elemIndex](Data-List.html#v:elemIndex "Data.List")
, by returning the indices of all elements equal to the query element, in ascending order.
>>>
elemIndices 'o' "Hello World"** **
[4,7]
findIndex :: (a -> Bool) -> [a] -> Maybe Int Source #
The [findIndex](Data-List.html#v:findIndex "Data.List")
function takes a predicate and a list and returns the index of the first element in the list satisfying the predicate, or [Nothing](Data-Maybe.html#v:Nothing "Data.Maybe")
if there is no such element.
>>>
**findIndex isSpace "Hello World!"** **
**Just 5
findIndices :: (a -> Bool) -> [a] -> [Int] Source #
The [findIndices](Data-List.html#v:findIndices "Data.List")
function extends [findIndex](Data-List.html#v:findIndex "Data.List")
, by returning the indices of all elements satisfying the predicate, in ascending order.
>>>
`` findIndices (elem
"aeiou") "Hello World!"
``[1,4,7]
zip :: [a] -> [b] -> [(a, b)] Source #
\(\mathcal{O}(\min(m,n))\). [zip](Data-List.html#v:zip "Data.List")
takes two lists and returns a list of corresponding pairs.
zip [1, 2] ['a', 'b'] = [(1, 'a'), (2, 'b')]
If one input list is short, excess elements of the longer list are discarded:
zip [1] ['a', 'b'] = [(1, 'a')] zip [1, 2] ['a'] = [(1, 'a')]
[zip](Data-List.html#v:zip "Data.List")
is right-lazy:
zip [] | = [] zip | [] = |
[zip](Data-List.html#v:zip "Data.List")
is capable of list fusion, but it is restricted to its first list argument and its resulting list.
zip3 :: [a] -> [b] -> [c] -> [(a, b, c)] Source #
[zip3](Data-List.html#v:zip3 "Data.List")
takes three lists and returns a list of triples, analogous to[zip](Data-List.html#v:zip "Data.List")
. It is capable of list fusion, but it is restricted to its first list argument and its resulting list.
zip4 :: [a] -> [b] -> [c] -> [d] -> [(a, b, c, d)] Source #
The [zip4](Data-List.html#v:zip4 "Data.List")
function takes four lists and returns a list of quadruples, analogous to [zip](Data-List.html#v:zip "Data.List")
. It is capable of list fusion, but it is restricted to its first list argument and its resulting list.
zip5 :: [a] -> [b] -> [c] -> [d] -> [e] -> [(a, b, c, d, e)] Source #
The [zip5](Data-List.html#v:zip5 "Data.List")
function takes five lists and returns a list of five-tuples, analogous to [zip](Data-List.html#v:zip "Data.List")
. It is capable of list fusion, but it is restricted to its first list argument and its resulting list.
zip6 :: [a] -> [b] -> [c] -> [d] -> [e] -> [f] -> [(a, b, c, d, e, f)] Source #
The [zip6](Data-List.html#v:zip6 "Data.List")
function takes six lists and returns a list of six-tuples, analogous to [zip](Data-List.html#v:zip "Data.List")
. It is capable of list fusion, but it is restricted to its first list argument and its resulting list.
zip7 :: [a] -> [b] -> [c] -> [d] -> [e] -> [f] -> [g] -> [(a, b, c, d, e, f, g)] Source #
The [zip7](Data-List.html#v:zip7 "Data.List")
function takes seven lists and returns a list of seven-tuples, analogous to [zip](Data-List.html#v:zip "Data.List")
. It is capable of list fusion, but it is restricted to its first list argument and its resulting list.
zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] Source #
\(\mathcal{O}(\min(m,n))\). [zipWith](Data-List.html#v:zipWith "Data.List")
generalises [zip](Data-List.html#v:zip "Data.List")
by zipping with the function given as the first argument, instead of a tupling function. For example, `[zipWith](Data-List.html#v:zipWith "Data.List")` (+)
is applied to two lists to produce the list of corresponding sums:
>>>
zipWith (+) [1, 2, 3] [4, 5, 6]** **
[5,7,9]
[zipWith](Data-List.html#v:zipWith "Data.List")
is right-lazy:
zipWith f [] | = []
[zipWith](Data-List.html#v:zipWith "Data.List")
is capable of list fusion, but it is restricted to its first list argument and its resulting list.
zipWith3 :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d] Source #
The [zipWith3](Data-List.html#v:zipWith3 "Data.List")
function takes a function which combines three elements, as well as three lists and returns a list of their point-wise combination, analogous to [zipWith](Data-List.html#v:zipWith "Data.List")
. It is capable of list fusion, but it is restricted to its first list argument and its resulting list.
zipWith4 :: (a -> b -> c -> d -> e) -> [a] -> [b] -> [c] -> [d] -> [e] Source #
The [zipWith4](Data-List.html#v:zipWith4 "Data.List")
function takes a function which combines four elements, as well as four lists and returns a list of their point-wise combination, analogous to [zipWith](Data-List.html#v:zipWith "Data.List")
. It is capable of list fusion, but it is restricted to its first list argument and its resulting list.
zipWith5 :: (a -> b -> c -> d -> e -> f) -> [a] -> [b] -> [c] -> [d] -> [e] -> [f] Source #
The [zipWith5](Data-List.html#v:zipWith5 "Data.List")
function takes a function which combines five elements, as well as five lists and returns a list of their point-wise combination, analogous to [zipWith](Data-List.html#v:zipWith "Data.List")
. It is capable of list fusion, but it is restricted to its first list argument and its resulting list.
zipWith6 :: (a -> b -> c -> d -> e -> f -> g) -> [a] -> [b] -> [c] -> [d] -> [e] -> [f] -> [g] Source #
The [zipWith6](Data-List.html#v:zipWith6 "Data.List")
function takes a function which combines six elements, as well as six lists and returns a list of their point-wise combination, analogous to [zipWith](Data-List.html#v:zipWith "Data.List")
. It is capable of list fusion, but it is restricted to its first list argument and its resulting list.
zipWith7 :: (a -> b -> c -> d -> e -> f -> g -> h) -> [a] -> [b] -> [c] -> [d] -> [e] -> [f] -> [g] -> [h] Source #
The [zipWith7](Data-List.html#v:zipWith7 "Data.List")
function takes a function which combines seven elements, as well as seven lists and returns a list of their point-wise combination, analogous to [zipWith](Data-List.html#v:zipWith "Data.List")
. It is capable of list fusion, but it is restricted to its first list argument and its resulting list.
unzip :: [(a, b)] -> ([a], [b]) Source #
[unzip](Data-List.html#v:unzip "Data.List")
transforms a list of pairs into a list of first components and a list of second components.
unzip3 :: [(a, b, c)] -> ([a], [b], [c]) Source #
The [unzip3](Data-List.html#v:unzip3 "Data.List")
function takes a list of triples and returns three lists, analogous to [unzip](Data-List.html#v:unzip "Data.List")
.
unzip4 :: [(a, b, c, d)] -> ([a], [b], [c], [d]) Source #
The [unzip4](Data-List.html#v:unzip4 "Data.List")
function takes a list of quadruples and returns four lists, analogous to [unzip](Data-List.html#v:unzip "Data.List")
.
unzip5 :: [(a, b, c, d, e)] -> ([a], [b], [c], [d], [e]) Source #
The [unzip5](Data-List.html#v:unzip5 "Data.List")
function takes a list of five-tuples and returns five lists, analogous to [unzip](Data-List.html#v:unzip "Data.List")
.
unzip6 :: [(a, b, c, d, e, f)] -> ([a], [b], [c], [d], [e], [f]) Source #
The [unzip6](Data-List.html#v:unzip6 "Data.List")
function takes a list of six-tuples and returns six lists, analogous to [unzip](Data-List.html#v:unzip "Data.List")
.
unzip7 :: [(a, b, c, d, e, f, g)] -> ([a], [b], [c], [d], [e], [f], [g]) Source #
The [unzip7](Data-List.html#v:unzip7 "Data.List")
function takes a list of seven-tuples and returns seven lists, analogous to [unzip](Data-List.html#v:unzip "Data.List")
.
Special listsFunctions on strings
lines :: String -> [String] Source #
[lines](Data-List.html#v:lines "Data.List")
breaks a string up into a list of strings at newline characters. The resulting strings do not contain newlines.
Note that after splitting the string at newline characters, the last part of the string is considered a line even if it doesn't end with a newline. For example,
>>>
lines ""** **
[]
>>>
lines "\n"** **
[""]
>>>
lines "one"** **
["one"]
>>>
lines "one\n"** **
["one"]
>>>
lines "one\n\n"** **
["one",""]
>>>
lines "one\ntwo"** **
["one","two"]
>>>
lines "one\ntwo\n"** **
["one","two"]
Thus `[lines](Data-List.html#v:lines "Data.List")` s
contains at least as many elements as newlines in s
.
words :: String -> [String] Source #
[words](Data-List.html#v:words "Data.List")
breaks a string up into a list of words, which were delimited by white space.
>>>
words "Lorem ipsum\ndolor"** **
["Lorem","ipsum","dolor"]
unlines :: [String] -> String Source #
[unlines](Data-List.html#v:unlines "Data.List")
is an inverse operation to [lines](Data-List.html#v:lines "Data.List")
. It joins lines, after appending a terminating newline to each.
>>>
unlines ["Hello", "World", "!"]** **
"Hello\nWorld\n!\n"
nub :: Eq a => [a] -> [a] Source #
\(\mathcal{O}(n^2)\). The [nub](Data-List.html#v:nub "Data.List")
function removes duplicate elements from a list. In particular, it keeps only the first occurrence of each element. (The name [nub](Data-List.html#v:nub "Data.List")
means `essence'.) It is a special case of [nubBy](Data-List.html#v:nubBy "Data.List")
, which allows the programmer to supply their own equality test.
>>>
nub [1,2,3,4,3,2,1,2,4,3,5]** **
[1,2,3,4,5]
delete :: Eq a => a -> [a] -> [a] Source #
\(\mathcal{O}(n)\). [delete](Data-List.html#v:delete "Data.List")
x
removes the first occurrence of x
from its list argument. For example,
>>>
delete 'a' "banana"** **
"bnana"
It is a special case of [deleteBy](Data-List.html#v:deleteBy "Data.List")
, which allows the programmer to supply their own equality test.
(\\) :: Eq a => [a] -> [a] -> [a] infix 5 Source #
The [\\](Data-List.html#v:-92--92- "Data.List")
function is list difference (non-associative). In the result of xs
[\\](Data-List.html#v:-92--92- "Data.List")
ys
, the first occurrence of each element ofys
in turn (if any) has been removed from xs
. Thus
(xs ++ ys) \ xs == ys.
>>>
"Hello World!" \\ "ell W"** **
"Hoorld!"
It is a special case of [deleteFirstsBy](Data-List.html#v:deleteFirstsBy "Data.List")
, which allows the programmer to supply their own equality test.
union :: Eq a => [a] -> [a] -> [a] Source #
The [union](Data-List.html#v:union "Data.List")
function returns the list union of the two lists. For example,
>>>
`` "dog" union
"cow"
``"dogcw"
Duplicates, and elements of the first list, are removed from the the second list, but if the first list contains duplicates, so will the result. It is a special case of [unionBy](Data-List.html#v:unionBy "Data.List")
, which allows the programmer to supply their own equality test.
intersect :: Eq a => [a] -> [a] -> [a] Source #
The [intersect](Data-List.html#v:intersect "Data.List")
function takes the list intersection of two lists. For example,
>>>
`` [1,2,3,4] intersect
[2,4,6,8]
``[2,4]
If the first list contains duplicates, so will the result.
>>>
`` [1,2,2,3,4] intersect
[6,4,4,2]
``[2,2,4]
It is a special case of [intersectBy](Data-List.html#v:intersectBy "Data.List")
, which allows the programmer to supply their own equality test. If the element is found in both the first and the second list, the element from the first list will be used.
sort :: Ord a => [a] -> [a] Source #
The [sort](Data-List.html#v:sort "Data.List")
function implements a stable sorting algorithm. It is a special case of [sortBy](Data-List.html#v:sortBy "Data.List")
, 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.
>>>
sort [1,6,4,3,2,5]** **
[1,2,3,4,5,6]
sortOn :: Ord b => (a -> b) -> [a] -> [a] Source #
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 from lowest to highest, keeping duplicates in the order they appeared in the input.
>>>
sortOn fst [(2, "world"), (4, "!"), (1, "Hello")]** **
[(1,"Hello"),(2,"world"),(4,"!")]
Since: 4.8.0.0
insert :: Ord a => a -> [a] -> [a] Source #
\(\mathcal{O}(n)\). The [insert](Data-List.html#v:insert "Data.List")
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](Data-List.html#v:insertBy "Data.List")
, which allows the programmer to supply their own comparison function.
>>>
insert 4 [1,2,3,5,6,7]** **
[1,2,3,4,5,6,7]
Generalized functionsThe "By" operations
By convention, overloaded functions have a non-overloaded counterpart whose name is suffixed with ` By
'.
It is often convenient to use these functions together with[on](Data-Function.html#v:on "Data.Function")
, for instance `[sortBy](Data-List.html#v:sortBy "Data.List")` (`[compare](Prelude.html#v:compare "Prelude")` `` [`on`](Data-Function.html#v:on "Data.Function") `` `[fst](Prelude.html#v:fst "Prelude")`)
.
User-supplied equality (replacing an Eq context)
The predicate is assumed to define an equivalence.
nubBy :: (a -> a -> Bool) -> [a] -> [a] Source #
The [nubBy](Data-List.html#v:nubBy "Data.List")
function behaves just like [nub](Data-List.html#v:nub "Data.List")
, except it uses a user-supplied equality predicate instead of the overloaded [==](Data-Eq.html#v:-61--61- "Data.Eq")
function.
>>>
nubBy (\x y -> mod x 3 == mod y 3) [1,2,4,5,6]** **
[1,2,6]
deleteBy :: (a -> a -> Bool) -> a -> [a] -> [a] Source #
\(\mathcal{O}(n)\). The [deleteBy](Data-List.html#v:deleteBy "Data.List")
function behaves like [delete](Data-List.html#v:delete "Data.List")
, but takes a user-supplied equality predicate.
>>>
deleteBy (<=) 4 [1..10]** **
[1,2,3,5,6,7,8,9,10]
deleteFirstsBy :: (a -> a -> Bool) -> [a] -> [a] -> [a] Source #
The [deleteFirstsBy](Data-List.html#v:deleteFirstsBy "Data.List")
function takes a predicate and two lists and returns the first list with the first occurrence of each element of the second list removed.
User-supplied comparison (replacing an Ord context)
The function is assumed to define a total ordering.
sortBy :: (a -> a -> Ordering) -> [a] -> [a] Source #
The [sortBy](Data-List.html#v:sortBy "Data.List")
function is the non-overloaded version of [sort](Data-List.html#v:sort "Data.List")
.
>>>
sortBy (\(a,_) (b,_) -> compare a b) [(2, "world"), (4, "!"), (1, "Hello")]** **
[(1,"Hello"),(2,"world"),(4,"!")]
maximumBy :: Foldable t => (a -> a -> Ordering) -> t a -> a Source #
The largest element of a non-empty structure with respect to the given comparison function.
minimumBy :: Foldable t => (a -> a -> Ordering) -> t a -> a Source #
The least element of a non-empty structure with respect to the given comparison function.
The prefix ` generic
' indicates an overloaded function that is a generalized version of a Prelude function.
genericLength :: Num i => [a] -> i Source #
\(\mathcal{O}(n)\). The [genericLength](Data-List.html#v:genericLength "Data.List")
function is an overloaded version of [length](GHC-List.html#v:length "GHC.List")
. In particular, instead of returning an [Int](Data-Int.html#t:Int "Data.Int")
, it returns any type which is an instance of [Num](Prelude.html#t:Num "Prelude")
. It is, however, less efficient than[length](GHC-List.html#v:length "GHC.List")
.
>>>
**genericLength [1, 2, 3] :: Int** **
**3
>>>
**genericLength [1, 2, 3] :: Float** **
**3.0