Append two vectors
[1,2,3,4] ++ [5,6]
A proof that some element is found in a vector
Vectors: Generic lists with explicit length in the type
the length of the list
the type of elements
Filter out Nothings from Vect
catMaybes [Just 1, Just 2, Nothing, Nothing, Just 5]
Flatten a vector of equal-length vectors
concat [[1,2,3], [4,5,6]]
Delete first element from list equal to the given one
delete 2 (fromList [1,2,2,3,4,4])
Construct a new vector consisting of all but the indicated element
deleteAt 1 [1,2,3,4]
Delete first element from list according to some test
deleteBy (<) 3 (fromList [1,2,2,3,4,4])
Get diagonal elements
diag [[1,2,3], [4,5,6], [7,8,9]]
Remove the first n elements of a Vect
the number of elements to remove
drop 2 [1,2,3,4]
drop (n + m) = drop m . drop n
Remove the element at the given position.
The vector to be removed from
A proof that the element to be removed is in the vector
Adding a prefix and then dropping the prefix does nothing. Or,
adding a suffix and then taking the suffix gets the suffix.
drop n . take (n + m) = take m . drop n
.
Or: there are two ways to extract a subsequence.
Drop up to the first n elements of a Vect if they exist.
the maximum number of elements to remove.
dropUpto 10 [1,2,3,4]
Remove the longest prefix of a Vect such that all removed elements satisfy some
Boolean predicate.
the predicate
dropWhile (<3) [1,2,3,4]
Use the default Boolean equality on elements to search for an item
what to search for
where to search
elem 3 [1,2,3,4]
Search for an item using a user-provided test
the equality test
the item to search for
the vector to search in
elemBy (==) 2 [1,2,3,4]
Find the index of the first element of the vector equal to the given one.
elemIndex 3 [1,2,3,4]
Find the index of the first element of the vector that satisfies some test
elemIndexBy (==) 3 [1,2,3,4]
Find the indices of all elements uquals to the given one
elemIndices 3 [1,2,3,4,3]
Find the indices of all elements that satisfy some test
elemIndicesBy (<=) 3 [1,2,3,4]
If the given Vect is the required length, return a Vect with that
length in its type, otherwise return Nothing
the required length
the vector with the desired length
Find all elements of a vector that satisfy some test
filter (< 3) (fromList [1,2,3,4])
Find the first element of the vector that satisfies some test
the test to satisfy
find (== 3) [1,2,3,4]
Find the index of the first element of the vector that satisfies some test
findIndex (== 3) [1,2,3,4]
Find the indices of all elements that satisfy some test
findIndices (< 3) [1,2,3,4]
Foldl without seeding the accumulator
foldl1 (-) (fromList [1,2,3])
Foldr without seeding the accumulator
foldr1 (-) (fromList [1,2,3])
Convert a list to a vector.
The length of the list should be statically known.
fromList [1,2,3,4]
Check if any element of xs is found in elems using the default Boolean equality test
hasAny [2,5] [1,2,3,4]
Check if any element of xs is found in elems by a user-provided comparison
the comparison operator
the vector to search
what to search for
hasAnyBy (==) [2,5] [1,2,3,4]
Only the first element of the vector
head [1,2,3,4]
Extract a particular element from a vector
index 1 [1,2,3,4]
All but the last element of the vector
init [1,2,3,4]
Insert an element at a particular index
insertAt 1 8 [1,2,3,4]
Alternate an element between the other elements of a vector
the element to intersperse
the vector to separate with sep
intersperse 0 [1,2,3,4]
A decision procedure for Elem
Verify vector prefix
isPrefixOf (fromList [1,2]) (fromList [1,2,3,4])
Verify vector prefix
isPrefixOfBy (==) (fromList [1,2]) (fromList [1,2,3,4])
Verify vector suffix
isSuffixOf (fromList [3,4]) (fromList [1,2,3,4])
Verify vector suffix
isSuffixOfBy (==) (fromList [3,4]) (fromList [1,2,3,4])
The last element of the vector
last [1,2,3,4]
Calculate the length of a Vect
.
Note: this is only useful if you don't already statically know the length
and you want to avoid matching the implicit argument for erasure reasons.
the length (provably equal to the return value)
the vector
Find the assocation of some key using the default Boolean equality test
lookup 3 [(1, 'a'), (2, 'b'), (3, 'c')]
Find the association of some key with a user-provided comparison
the comparison operator for keys (True if they match)
the key to look for
lookupBy (==) 2 [(1, 'a'), (2, 'b'), (3, 'c')]
Map a partial function across a vector, returning those elements for which
the function had a value.
The first projection of the resulting pair (ie the length) will always be
at most the length of the input vector. This is not, however, guaranteed
by the type.
the partial function (expressed by returning Maybe
)
the vector to check for results
mapMaybe ((find (=='a')) . unpack) (fromList ["abc","ade","bgh","xyz"])
Convert Maybe type into Vect
maybeToVect (Just 2)
Merge two ordered vectors
mergeBy compare (fromList [1,3,5]) (fromList [2,3,4,5,6])
An item not in the head and not in the tail is not in the Vect at all
Nothing can be in an empty Vect
Make the elements of some vector unique by the default Boolean equality
nub (fromList [1,2,2,3,4,4])
Make the elements of some vector unique by some test
nubBy (==) (fromList [1,2,2,3,4,4])
If the given Vect is at least the required length, return a Vect with
at least that length in its type, otherwise return Nothing
the required length
the vector with the desired length
A tuple where the first element is a Vect
of the n
elements passing given test
and the second element is a Vect
of the remaining elements of the original.
partition (== 2) (fromList [1,2,3,2,4])
Replace an element at a particlar index with another
replaceAt 1 8 [1,2,3,4]
Repeat some value some number of times.
the number of times to repeat it
the value to repeat
replicate 4 1
Reverse the order of the elements of a vector
reverse [1,2,3,4]
The scanl function is similar to foldl, but returns all the intermediate
accumulator states in the form of a vector.
scanl (-) 0 (fromList [1,2,3])
The scanl1 function is a variant of scanl that doesn't require an explicit
starting value.
It assumes the first element of the vector to be the starting value and then
starts the fold with the element following it.
scanl1 (-) (fromList [1,2,3])
A tuple where the first element is a Vect
of the n
first elements and
the second element is a Vect
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 Vect
to split in two
splitAt 2 (fromList [1,2,3,4])
All but the first element of the vector
tail [1,2,3,4]
Get the first n elements of a Vect
the number of elements to take
take 2 [1,2,3,4]
A Vect
may be restored from its components.
Adding a prefix and then taking the prefix gets the prefix. Or,
adding a suffix and then dropping the suffix does nothing.
take n . take (n + m) = take n
Take up to the first n elements of a Vect if they exist.
the maximum number of elements to take
takeUpto 10 [1,2,3,4]
Take the longest prefix of a Vect such that all elements satisfy some
Boolean predicate.
the predicate
takeWhile (<3) [1,2,3,4]
Transpose a Vect
of Vect
s, turning rows into columns and vice versa.
This is like zipping all the inner Vect
s together and is equivalent to traverse id
(transposeTraverse
).
As the types ensure rectangularity, this is an involution, unlike Prelude.List.transpose
.
transpose [[1,2], [3,4], [5,6], [7,8]]
Convert a vector of pairs to a pair of vectors
unzip (fromList [(1,2), (1,2)])
Convert a vector of three-tuples to a triplet of vectors
unzip3 (fromList [(1,2,3), (1,2,3)])
Replace the element at a particular index with the result of applying a function to it
the index to replace at
the update function
the vector to replace in
updateAt 1 (+10) [1,2,3,4]
Convert first element of Vect (if exists) into Maybe.
vectToMaybe [2]
Combine two equal-length vectors pairwise
zip (fromList [1,2,3,4]) (fromList [1,2,3,4])
Combine three equal-length vectors elementwise into a vector of tuples
zip3 (fromList [1,2,3,4]) (fromList [1,2,3,4]) (fromList [1,2,3,4])
Combine two equal-length vectors pairwise with some function.
the function to combine elements with
the first vector of elements
the second vector of elements
zipWith (+) (fromList [1,2,3,4]) (fromList [5,6,7,8])
Combine three equal-length vectors into a vector with some function
zipWith3 (\x,y,z => x+y+z) (fromList [1,2,3,4]) (fromList [5,6,7,8]) (fromList [1,1,1,1])