Module Data.List

Library with some useful operations on lists.

Category
general
Author
Michael Hanus, Bjoern Peemoeller
Version
November 2020

Exported Functions: \\, cycle, delete, deleteBy, diagonal, elemIndex, elemIndices, find, findIndex, findIndices, group, groupBy, init, inits, insertBy, intercalate, intersect, intersectBy, intersperse, isInfixOf, isPrefixOf, isSuffixOf, last, mapAccumL, mapAccumR, maximum, maximumBy, minimum, minimumBy, nub, nubBy, partition, permutations, product, replace, scanl, scanl1, scanr, scanr1, sort, sortBy, split, splitOn, sum, tails, transpose, unfoldr, union, unionBy


Exported Functions


elemIndex :: Eq a => a -> [a] -> Maybe Int  Deterministic 

Returns the index i of the first occurrence of an element in a list as (Just i), otherwise Nothing is returned.


elemIndices :: Eq a => a -> [a] -> [Int]  Deterministic 

Returns the list of indices of occurrences of an element in a list.


find :: (a -> Bool) -> [a] -> Maybe a  Deterministic 

Returns the first element e of a list satisfying a predicate as (Just e), otherwise Nothing is returned.


findIndex :: (a -> Bool) -> [a] -> Maybe Int  Deterministic 

Returns the index i of the first occurrences of a list element satisfying a predicate as (Just i), otherwise Nothing is returned.


findIndices :: (a -> Bool) -> [a] -> [Int]  Deterministic 

Returns the list of indices of list elements satisfying a predicate.


nub :: Eq a => [a] -> [a]  Deterministic 

Removes all duplicates in the argument list.


nubBy :: (a -> a -> Bool) -> [a] -> [a]  Deterministic 

Removes all duplicates in the argument list according to an equivalence relation.


delete :: Eq a => a -> [a] -> [a]  Deterministic 

Deletes the first occurrence of an element in a list.


deleteBy :: (a -> a -> Bool) -> a -> [a] -> [a]  Deterministic 

Deletes the first occurrence of an element in a list according to an equivalence relation.


(\\) :: Eq a => [a] -> [a] -> [a]  Deterministic 

Computes the difference of two lists.

:: Eq a
=> [a]  a list
-> [a]  a list
-> [a]  the list where the first occurrence of each element of ys has been removed from xs
Further infos:
  • defined as non-associative infix operator with precedence 5

union :: Eq a => [a] -> [a] -> [a]  Deterministic 

Computes the union of two lists.


unionBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]  Deterministic 

Computes the union of two lists according to the given equivalence relation


intersect :: Eq a => [a] -> [a] -> [a]  Deterministic 

Computes the intersection of two lists.


intersectBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]  Deterministic 

Computes the intersection of two lists according to the given equivalence relation


intersperse :: a -> [a] -> [a]  Deterministic 

Puts a separator element between all elements in a list.

Example: (intersperse 9 [1,2,3,4]) = [1,9,2,9,3,9,4]

Further infos:
  • solution complete, i.e., able to compute all solutions

intercalate :: [a] -> [[a]] -> [a]  Deterministic 

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.


transpose :: [[a]] -> [[a]]  Deterministic 

Transposes the rows and columns of the argument.

Example: (transpose [[1,2,3],[4,5,6]]) = [[1,4],[2,5],[3,6]]


diagonal :: [[a]] -> [a]  Deterministic 

Diagonalization of a list of lists. Fairly merges (possibly infinite) list of (possibly infinite) lists.

:: [[a]]  lists of lists
-> [a]  fair enumeration of all elements of inner lists of given lists

permutations :: [a] -> [[a]]  Deterministic 

Returns the list of all permutations of the argument.


partition :: (a -> Bool) -> [a] -> ([a], [a])  Deterministic 

Partitions a list into a pair of lists where the first list contains those elements that satisfy the predicate argument and the second list contains the remaining arguments.

Example: (partition (<4) [8,1,5,2,4,3]) = ([1,2,3],[8,5,4])


group :: Eq a => [a] -> [[a]]  Deterministic 

Splits the list argument into a list of lists of equal adjacent elements.

Example: (group [1,2,2,3,3,3,4]) = [[1],[2,2],[3,3,3],[4]]


groupBy :: (a -> a -> Bool) -> [a] -> [[a]]  Deterministic 

Splits the list argument into a list of lists of related adjacent elements.

:: a -> a -> Bool  the relation to classify adjacent elements
-> [a]  the list of elements
-> [[a]]  the list of lists of related adjacent elements

splitOn :: Eq a => [a] -> [a] -> [[a]]  Deterministic 

Breaks the second list argument into pieces separated by the first list argument, consuming the delimiter. An empty delimiter is invalid, and will cause an error to be raised.


split :: (a -> Bool) -> [a] -> [[a]]  Deterministic 

Splits a list into components delimited by separators, where the predicate returns True for a separator element. The resulting components do not contain the separators. Two adjacent separators result in an empty component in the output.

split (==[a](#a)) "aabbaca" == ["","","bb","c",""]
split (==[a](#a)) ""        == [""]

Further infos:
  • partially defined

inits :: [a] -> [[a]]  Deterministic 

Returns all initial segments of a list, starting with the shortest. Example: inits [1,2,3] == [[],[1],[1,2],[1,2,3]]

:: [a]  the list of elements
-> [[a]]  the list of initial segments of the argument list

tails :: [a] -> [[a]]  Deterministic 

Returns all final segments of a list, starting with the longest. Example: tails [1,2,3] == [[1,2,3],[2,3],[3],[]]

Further infos:
  • solution complete, i.e., able to compute all solutions

replace :: a -> Int -> [a] -> [a]  Deterministic 

Replaces an element in a list.

:: a  the new element
-> Int  the position of the new element (head = 0)
-> [a]  the old list
-> [a]  the new list where the p. element is replaced by x
Further infos:
  • partially defined

isPrefixOf :: Eq a => [a] -> [a] -> Bool  Deterministic 

Checks whether a list is a prefix of another.

:: Eq a
=> [a]  a list
-> [a]  a list
-> Bool  True if xs is a prefix of ys

isSuffixOf :: Eq a => [a] -> [a] -> Bool  Deterministic 

Checks whether a list is a suffix of another.

:: Eq a
=> [a]  a list
-> [a]  a list
-> Bool  True if xs is a suffix of ys

isInfixOf :: Eq a => [a] -> [a] -> Bool  Deterministic 

Checks whether a list is contained in another.

:: Eq a
=> [a]  a list
-> [a]  a list
-> Bool  True if xs is contained in ys

sort :: Ord a => [a] -> [a]  Deterministic 

The default sorting operation, mergeSort, with standard ordering <=.


sortBy :: (a -> a -> Bool) -> [a] -> [a]  Deterministic 

Sorts a list w.r.t. an ordering relation by the insertion method.


insertBy :: (a -> a -> Bool) -> a -> [a] -> [a]  Deterministic 

Inserts an object into a list according to an ordering relation.

:: a -> a -> Bool  an ordering relation (e.g., less-or-equal)
-> a  an element
-> [a]  a list
-> [a]  a list where the element has been inserted

last :: [a] -> a  Deterministic 

Returns the last element of a non-empty list.

Further infos:
  • partially defined

init :: [a] -> [a]  Deterministic 

Returns the input list with the last element removed.

Further infos:
  • partially defined

sum :: Num a => [a] -> a  Deterministic 

Returns the sum of a list of integers.


product :: Num a => [a] -> a  Deterministic 

Returns the product of a list of integers.


maximum :: Ord a => [a] -> a  Deterministic 

Returns the maximum of a non-empty list.

Further infos:
  • partially defined

maximumBy :: (a -> a -> Ordering) -> [a] -> a  Deterministic 

Returns the maximum of a non-empty list according to the given comparison function

Further infos:
  • partially defined

minimum :: Ord a => [a] -> a  Deterministic 

Returns the minimum of a non-empty list.

Further infos:
  • partially defined

minimumBy :: (a -> a -> Ordering) -> [a] -> a  Deterministic 

Returns the minimum of a non-empty list according to the given comparison function

Further infos:
  • partially defined

scanl :: (a -> b -> a) -> a -> [b] -> [a]  Deterministic 

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, ...]


scanl1 :: (a -> a -> a) -> [a] -> [a]  Deterministic 

scanl1 is a variant of scanl that has no starting value argument: scanl1 f [x1, x2, ...] == [x1, x1 f x2, ...]


scanr :: (a -> b -> b) -> b -> [a] -> [b]  Deterministic 

scanr is the right-to-left dual of scanl.


scanr1 :: (a -> a -> a) -> [a] -> [a]  Deterministic 

scanr1 is a variant of scanr that has no starting value argument.


mapAccumL :: (a -> b -> (a, c)) -> a -> [b] -> (a, [c])  Deterministic 

The mapAccumL function behaves like a combination of map and foldl; it applies a function to each element of a list, passing an accumulating parameter from left to right, and returning a final value of this accumulator together with the new list.


mapAccumR :: (a -> b -> (a, c)) -> a -> [b] -> (a, [c])  Deterministic 

The mapAccumR function behaves like a combination of map and foldr; it applies a function to each element of a list, passing an accumulating parameter from right to left, and returning a final value of this accumulator together with the new list.


cycle :: [a] -> [a]  Deterministic 

Builds an infinite list from a finite one.

Further infos:
  • partially defined

unfoldr :: (a -> Maybe (b, a)) -> a -> [b]  Deterministic 

Builds a list from a seed value.