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
Bifunctor
type class for bothListP
andListE
. Thebimap
method allows you to map one function over all of the values of typea
and another function over all of the values of typeb
.class Bifunctor t where bimap :: (a -> c) -> (b -> d) -> t a b -> t c d
Implement two functions
mapL
andmapR
that 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
Bifoldable
type class for bothListP
andListE
. Thebifoldr
method allows you to accumulate values of typea
and of typeb
to the same accumulator typec
.class Bifoldable t where bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> t a b -> c
Implement two functions
foldrL
andfoldrR
that 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
bifoldMap
that maps each element in aBifoldable
instance to a common monoid and combine the results usingmappend
. This function is used by thecheckAll
andtoEitherList
functions in the template.You’ll need to use the
Monoid
type class in your solution, which you can look up in the HaskellPrelude
. If you get stuck, consider looking at the relatedfoldMap
method from theFoldable
type class.