Hasklet #3
Due: Mon, May 3
Read the general information on Hasklets!
Submit your solution through Canvas.
Description
Consider the following two list-like data types. The first defines a data structure that is isomorphic (i.e. structurally equivalent) to [(a,b)]. The second defines a data structure that is isomorphic to [Either a b].
-- | A list of pairs of elements of type a AND b.
data ListP a b = NilP
               | ConsP a b (ListP a b)
-- | A list of elements of either type a OR b.
data ListE a b = NilE
               | ConsL a (ListE a b)
               | ConsR b (ListE a b)Tasks
The template file contains cursory test cases for each of the tasks. As always, feel free to extend these with additional tests.
Instantiate the following
Bifunctortype class for bothListPandListE. Thebimapmethod allows you to map one function over all of the values of typeaand another function over all of the values of typeb.class Bifunctor t where bimap :: (a -> c) -> (b -> d) -> t a b -> t c dImplement two functions
mapLandmapRthat work on both kinds of lists. These functions should map a single function over values of either the left type variable or the right type variable, respectively. Make sure to write the type of each function.Instantiate the following
Bifoldabletype class for bothListPandListE. Thebifoldrmethod allows you to accumulate values of typeaand of typebto the same accumulator typec.class Bifoldable t where bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> t a b -> cImplement two functions
foldrLandfoldrRthat work on both kinds of lists. These functions should map a single accumulator function over values of either the left type variable or the right type variable, respectively. Make sure to write the type of each function.Implement the function
bifoldMapthat maps each element in aBifoldableinstance to a common monoid and combine the results usingmappend. This function is used by thecheckAllandtoEitherListfunctions in the template.You’ll need to use the
Monoidtype class in your solution, which you can look up in the HaskellPrelude. If you get stuck, consider looking at the relatedfoldMapmethod from theFoldabletype class.