Exercises
The exercises in this section are supposed to increase you experience in writing purely functional code. In some cases it might be useful to use let expressions or where blocks, but this will not always be required.
Exercise 3 is again of utmost importance. traverseList is a specialized version of the more general traverse, one of the most powerful and versatile functions available in the Prelude (check out its type!).
-
Module
Data.Listin base exports functionsfindandelem. Inspect their types and use these in the implementation ofhandleRequest. This should allow you to completely get rid of thewhereblock. -
Refactor
handleRequestto useEither, such thathandleRequest : DB -> Request -> Either Failure Album, wheredata Failure : Type where UnknownUser : Email -> Failure InvalidPassword : Failure AccessDenied : Email -> Album -> FailureHint: You may find nested
casestatements helpful. -
Define an enumeration type listing the four nucleobases occurring in DNA strands. Define also a type alias
DNAfor lists of nucleobases. Declare and implement functionreadBasefor converting a single character (typeChar) to a nucleobase. You can use character literals in your implementation like so:'A','a'. Note, that this function might fail, so adjust the result type accordingly. -
Implement the following function, which tries to convert all values in a list with a function, which might fail. The result should be a
Justholding the list of converted values in unmodified order, if and only if every single conversion was successful.traverseList : (a -> Maybe b) -> List a -> Maybe (List b)You can verify, that the function behaves correctly with the following test:
traverseList Just [1,2,3] = Just [1,2,3]. -
Implement function
readDNA : String -> Maybe DNAusing the functions and types defined in exercises 2 and 3. You will also need functionunpackfrom the Prelude. -
Implement function
complement : DNA -> DNAto calculate the complement of a strand of DNA.