Exercises part 2
-
Implement function
update
, which, given a function of typea -> a
, updates the value in aVect n a
at positionk < n
. -
Implement function
insert
, which inserts a value of typea
at positionk <= n
in aVect n a
. Note, thatk
is the index of the freshly inserted value, so that the following holds:index k (insert k v vs) = v
-
Implement function
delete
, which deletes a value from a vector at the given index.This is trickier than Exercises 1 and 2, as we have to properly encode in the types that the vector is getting one element shorter.
-
We can use
Fin
to implement safe indexing intoList
s as well. Try to come up with a type and implementation forsafeIndexList
.Note: If you don't know how to start, look at the type of
fromList
for some inspiration. You might also need give the arguments in a different order than forindex
. -
Implement function
finToNat
, which converts aFin n
to the corresponding natural number, and use this to declare and implement functiontake
for splitting of the firstk
elements of aVect n a
withk <= n
. -
Implement function
minus
for subtracting a valuek
from a natural numbern
withk <= n
. -
Use
minus
from Exercise 6 to declare and implement functiondrop
, for dropping the firstk
values from aVect n a
, withk <= n
. -
Implement function
splitAt
for splitting aVect n a
at positionk <= n
, returning the prefix and suffix of the vector wrapped in a pair.Hint: Use
take
anddrop
in your implementation.
Hint: Since Fin n
consists of the values strictly smaller than n
, Fin (S n)
consists of the values smaller than or equal to n
.
Note: Functions take
, drop
, and splitAt
, while correct and provably total, are rather cumbersome to type. There is an alternative way to declare their types, as we will see in the next section.