Append two lists
Satisfiable if k
is a valid index into xs
the potential index
the list into which k may be an index
Generic lists
Satisfiable if xs
is non-empty (e.g., if xs
is a cons).
The \\
function is list difference (non-associative). In the result of
xs \\ ys
, the first occurrence of each element of ys in turn (if any) has
been removed from xs
, e.g.,
(([1,2] ++ [2,3]) \\ [1,2])
Appending lists is associative.
Appending pairwise equal lists gives equal lists
The empty list is a right identity for append.
Given a list and a predicate, returns a pair consisting of the longest
prefix of the list that does not satisfy a predicate and the rest of the
list.
break (>=3) [1,2,3,2,1]
Keep the Just
elements in a list, discarding the Nothing
elements.
Two lists are equal, if their heads are equal and their tails are equal.
(::) is injective
delete x
removes the first occurrence of x
from its list argument. For
example,
delete 'a' ['b', 'a', 'n', 'a', 'n', 'a']
It is a special case of deleteBy, which allows the programmer to supply
their own equality test.
The deleteBy function behaves like delete, but takes a user-supplied equality predicate.
Drop the first n
elements of xs
If there are not enough elements, return the empty list.
how many elements to drop
the list to drop them from
Remove the longest prefix of a list such that all removed elements satisfy some
Boolean predicate.
the predicate
Check if something is a member of a list using the default Boolean equality.
Check if something is a member of a list using a custom comparison.
Find the index of the first occurrence of an element in a list,
using the default equality test for the type of list elements.
Find the index of the first occurrence of an element in a list, using a custom equality test.
Find all indices for an element in a list, using the default equality test for the type of list elements.
Find all indices for an element in a list, using a custom equality test.
filter, applied to a predicate and a list, returns the list of those
elements that satisfy the predicate; e.g.,
filter (< 3) [Z, S Z, S (S Z), S (S (S Z)), S (S (S (S Z)))]
A filtered list is no longer than its input
Find the first element of a list that satisfies a predicate, or Nothing
if none do.
Find the index of the first element of a list that satisfies a predicate, or
Nothing
if none do.
Find the indices of all elements that satisfy some predicate.
Implement foldl using foldr, for a later equality proof.
The definition of foldl works the same as the default definition
in terms of foldr
Check if any elements of the first list are found in the second, using
Boolean equality.
Check if any elements of the first list are found in the second, using
a custom comparison.
No list contains an element of the empty list by any predicate.
No list contains an element of the empty list.
Get the first element of a non-empty list
proof that the list is non-empty
Attempt to get the first element of a list. If the list is empty, return
Nothing
.
Decide whether k
is a valid index into xs
Find a particular element of a list.
a proof that the index is within bounds
Attempt to find a particular element of a list.
If the provided index is out of bounds, return Nothing.
Return all but the last element of a non-empty list.
proof that the list is non-empty
Attempt to Return all but the last element of a list.
If the list is empty, return Nothing
.
The inits function returns all initial segments of the argument, shortest
first. For example,
inits [1,2,3]
Given a separator list and some more lists, produce a new list by
placing the separator between each of the lists.
the separator
the lists between which the separator will be placed
Insert some separator between the elements of a list.
with List (intersperse ',' ['a', 'b', 'c', 'd', 'e'])
Returns True
iff the argument is not empty
The isInfixOf function takes two lists and returns True iff the first list
is contained, wholly and intact, anywhere within the second.
isInfixOf ['b','c'] ['a', 'b', 'c', 'd']
isInfixOf ['b','d'] ['a', 'b', 'c', 'd']
Returns True
iff the argument is empty
The isPrefixOf function takes two lists and returns True iff the first list is a prefix of the second.
Check whether a list is a prefix of another according to a user-supplied equality test.
the equality comparison
the potential prefix
the list that may have left
as its prefix
The isSuffixOf function takes two lists and returns True iff the first list is a suffix of the second.
Retrieve the last element of a non-empty list.
proof that the list is non-empty
Attempt to retrieve the last element of a non-empty list.
If the list is empty, return Nothing
.
Compute the length of a list.
Runs in linear time
The length of two lists that are appended is the sum of the lengths
of the input lists.
Simply-typed recursion operator for lists.
what to return at the end of the list
what to do at each step of recursion
the list to recurse over
Either return the head of a list, or Nothing
if it is empty.
Find associated information in a list using Boolean equality.
Find associated information in a list using a custom comparison.
List.map is distributive over appending.
Mapping a function over two lists and appending them is equivalent
to appending them and then mapping the function.
Mapping two functions is the same as mapping their composition.
Apply a partial function to the elements of a list, keeping the ones at which
it is defined.
Mapping a function over a list doesn't change its length.
Merge two sorted lists using the default ordering for the type of their elements.
Merge two sorted lists using an arbitrary comparison
predicate. Note that the lists must have been sorted using this
predicate already.
Decide whether a list is non-empty
O(n^2). The nub function removes duplicate elements from a list. In
particular, it keeps only the first occurrence of each element. It is a
special case of nubBy, which allows the programmer to supply their own
equality test.
nub (the (List _) [1,2,1,3])
The nubBy function behaves just like nub, except it uses a user-supplied
equality predicate instead of the overloaded == function.
The partition function takes a predicate a list and returns the pair of
lists of elements which do and do not satisfy the predicate, respectively;
e.g.,
partition (<3) [0, 1, 2, 3, 4, 5]
Replaces all occurences of the first argument with the second argument in a list.
replaceOn '-' ',' ['1', '-', '2', '-', '3']
Construct a list with n
copies of x
how many copies
the element to replicate
Return the elements of a list in reverse order.
Reverse a list onto an existing tail.
The scanl function is similar to foldl, but returns all the intermediate
accumulator states in the form of a list.
scanl (+) 0 [1,2,3,4]
The scanl1 function is a variant of scanl that doesn't require an explicit
starting value.
It assumes the first element of the list to be the starting value and then
starts the fold with the element following it.
scanl1 (+) [1,2,3,4]
Sort a list using the default ordering for the type of its elements.
Sort a list using some arbitrary comparison predicate.
how to compare elements
the list to sort
Check whether a list is sorted with respect to the default ordering for the type of its elements.
Given a list and a predicate, returns a pair consisting of the longest
prefix of the list that satisfies a predicate and the rest of the list.
span (<3) [1,2,3,2,1]
Split on any elements that satisfy the given predicate.
split (<2) [2,0,3,1,4]
A tuple where the first element is a List
of the n
first elements and
the second element is a List
of the remaining elements of the original.
It is equivalent to (take n xs, drop n xs)
(splitAtTakeDrop
),
but is more efficient.
the index to split at
the List
to split in two
Split on the given element.
splitOn 0 [1,0,2,0,0,3]
Get the tail of a non-empty list.
proof that the list is non-empty
Attempt to get the tail of a list.
If the list is empty, return Nothing
.
The tails function returns all final segments of the argument, longest
first. For example,
tails [1,2,3] == [[1,2,3], [2,3], [3], []]
Take the first n
elements of xs
If there are not enough elements, return the whole list.
how many elements to take
the list to take them from
Take the longest prefix of a list such that all elements satisfy some
Boolean predicate.
the predicate
Convert any Foldable structure to a list.
Transposes rows and columns of a list of lists.
with List transpose [[1, 2], [3, 4]]
This also works for non square scenarios, thus
involution does not always hold:
transpose [[], [1, 2]] = [[1], [2]]
transpose (transpose [[], [1, 2]]) = [[1, 2]]
Compute the union of two lists according to their Eq
implementation.
union ['d', 'o', 'g'] ['c', 'o', 'w']
The unionBy function returns the union of two lists by user-supplied equality predicate.
Split a list of pairs into two lists
Split a list of triples into three lists
Combine two lists elementwise into pairs
Combine three lists elementwise into tuples
Combine two lists elementwise using some function. If they are different
lengths, the result is truncated to the length of the shorter list.
the function to combine elements with
the first list
the second list
Combine three lists elementwise using some function. If they are different
lengths, the result is truncated to the length of the shortest list.
the function to combine elements with
the first list
the second list
the third list