Library with some useful operations on lists.
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
elemIndex
:: Eq a => a -> [a] -> Maybe Int
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]
Returns the list of indices of occurrences of an element in a list.
find
:: (a -> Bool) -> [a] -> Maybe a
Returns the first element e
of a list satisfying a predicate
as (Just e)
,
otherwise Nothing
is returned.
findIndex
:: (a -> Bool) -> [a] -> Maybe Int
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]
Returns the list of indices of list elements satisfying a predicate.
Removes all duplicates in the argument list.
nubBy
:: (a -> a -> Bool) -> [a] -> [a]
Removes all duplicates in the argument list according to an equivalence relation.
delete
:: Eq a => a -> [a] -> [a]
Deletes the first occurrence of an element in a list.
deleteBy
:: (a -> a -> Bool) -> a -> [a] -> [a]
Deletes the first occurrence of an element in a list according to an equivalence relation.
(\\)
:: Eq a => [a] -> [a] -> [a]
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
|
union
:: Eq a => [a] -> [a] -> [a]
Computes the union of two lists.
unionBy
:: (a -> a -> Bool) -> [a] -> [a] -> [a]
Computes the union of two lists according to the given equivalence relation
intersect
:: Eq a => [a] -> [a] -> [a]
Computes the intersection of two lists.
intersectBy
:: (a -> a -> Bool) -> [a] -> [a] -> [a]
Computes the intersection of two lists according to the given equivalence relation
intersperse
:: a -> [a] -> [a]
Puts a separator element between all elements in a list.
Example: (intersperse 9 [1,2,3,4]) = [1,9,2,9,3,9,4]
intercalate
:: [a] -> [[a]] -> [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.
transpose
:: [[a]] -> [[a]]
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]
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]]
Returns the list of all permutations of the argument.
partition
:: (a -> Bool) -> [a] -> ([a], [a])
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])
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]]
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]]
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]]
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)) "" == [""]
inits
:: [a] -> [[a]]
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]]
Returns all final segments of a list, starting with the longest.
Example: tails [1,2,3] == [[1,2,3],[2,3],[3],[]]
replace
:: a -> Int -> [a] -> [a]
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
|
isPrefixOf
:: Eq a => [a] -> [a] -> Bool
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
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
Checks whether a list is contained in another.
:: Eq a
|
|
=> [a]
|
a list |
-> [a]
|
a list |
-> Bool
|
True if xs is contained in ys |
The default sorting operation, mergeSort, with standard ordering <=
.
sortBy
:: (a -> a -> Bool) -> [a] -> [a]
Sorts a list w.r.t. an ordering relation by the insertion method.
insertBy
:: (a -> a -> Bool) -> a -> [a] -> [a]
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
Returns the last element of a non-empty list.
init
:: [a] -> [a]
Returns the input list with the last element removed.
Returns the sum of a list of integers.
Returns the product of a list of integers.
Returns the maximum of a non-empty list.
maximumBy
:: (a -> a -> Ordering) -> [a] -> a
Returns the maximum of a non-empty list according to the given comparison function
Returns the minimum of a non-empty list.
minimumBy
:: (a -> a -> Ordering) -> [a] -> a
Returns the minimum of a non-empty list according to the given comparison function
scanl
:: (a -> b -> a) -> a -> [b] -> [a]
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]
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]
scanr
is the right-to-left dual of scanl
.
scanr1
:: (a -> a -> a) -> [a] -> [a]
scanr1
is a variant of scanr
that has no starting value argument.
mapAccumL
:: (a -> b -> (a, c)) -> a -> [b] -> (a, [c])
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])
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]
Builds an infinite list from a finite one.
unfoldr
:: (a -> Maybe (b, a)) -> a -> [b]
Builds a list from a seed value.