I’ve been reading about applicative functors, notably in the Functional Pearl by McBride and Paterson. But I’d like to solidify my understanding by doing some exercises. I’d prefer programming exercises but proof exercises are OK too.
What exercises will help me learn to program effectively with applicative functors?
Individual exercises are OK as are pointers to exercises listed elsewhere.
It seems amusing to post some questions as an answer. This is a fun one, on the interplay between
ApplicativeandTraversable, based on sudoku.(1) Consider
Construct
so that the
Applicativeinstance does “vectorization” and theTraversableinstance works left-to-right. Don’t forget to construct a suitableFunctorinstance: check that you can extract this from either of theApplicativeor theTraversableinstance. You may finduseful for the latter.
(2) Consider
Show that
Now define
Suppose we represent a
Boardas a vertical zone of horizontal zonesShow how to rearrange it as a horizontal zone of vertical zones, and as a square of squares, using the functionality of
traverse.(3) Consider
or some other suitable construction (noting that the library
Monoidbehaviour for |Maybe| is inappropriate). Constructand implement
which consumes and delivers a character if it is accepted by a given predicate.
(4) Implement a parser which consumes any amount of whitespace, followed by a single digit (0 represents blanks)
Use
pureandtraverseto construct(5) Consider the constant functors
and construct
then use
traverseto implementConstruct
newtypewrappers forBoolexpressing its conjunctive and disjunctive monoid structures. Usecrushto implement versions ofanyandallwhich work for anyTraversablefunctor.(6) Implement
computing the list of values which occur more than once. (Not completely trivial.) (There’s a lovely way to do this using differential calculus, but that’s another story.)
(7) Implement
which check if a board is (1) full only of digits in [1..9] and (2) devoid of duplicates in any row, column or box.