Exercises part 2
-
Implement function
update, which, given a function of typea -> a, updates the value in aVect n aat positionk < n. -
Implement function
insert, which inserts a value of typeaat positionk <= nin aVect n a. Note, thatkis 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
Finto implement safe indexing intoLists 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
fromListfor some inspiration. You might also need give the arguments in a different order than forindex. -
Implement function
finToNat, which converts aFin nto the corresponding natural number, and use this to declare and implement functiontakefor splitting of the firstkelements of aVect n awithk <= n. -
Implement function
minusfor subtracting a valuekfrom a natural numbernwithk <= n. -
Use
minusfrom Exercise 6 to declare and implement functiondrop, for dropping the firstkvalues from aVect n a, withk <= n. -
Implement function
splitAtfor splitting aVect n aat positionk <= n, returning the prefix and suffix of the vector wrapped in a pair.Hint: Use
takeanddropin 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.