Week 8 Exercises
This week we will look at zippers and their interesting connections to calculus.
Exercise 1
Zippers are essentially suspended traversals over data structures. They are very useful for long-running programs like user interfaces, because it is often necessary to momentarily interrupt normal operation to interact with the user or communicate over the network. What makes zippers particularly interesting is that they can be derived mechanically by differentiating the corresponding types.
Differentiate the following types to obtain the types of their zippers.
- Derivative of
Maybe a
. - Derivative of
Join (,) a
. - Derivative of
[] a
. - Derivative of
Stream a
from thestreams
package. - Derivative of
Tree a
from thebinary-tree
package. - Derivative of
Tree a
from thecontainers
package.
We will need these to do exercise 2 as well.
module Week8.Exercise1 where
import Data.Stream.Infinite (Stream (..))
import qualified Data.Tree as Rose (Forest (..), Tree (..))
import qualified Data.Tree.Binary.Inorder as Binary (Tree (..))
Exercise 2
Define navigation functions for the zippers of the following types. That is, for each type A
with a zipper of type ZA
, implement a function of type MonadThrow m => A -> m ZA
that tries to create a zipper and functions of type MonadThrow m => ZA -> m ZA
that try to move the zipper around.
- Zipper for
Maybe a
. - Zipper for
Join (,) a
. - Zipper for
[] a
. - Zipper for
Stream a
from thestreams
package. - Zipper for
Tree a
from thebinary-tree
package. - Zipper for
Tree a
from thecontainers
package.
The Monad
constraint should allow you to use do
notation to ensure your navigation functions can really reach any position.
module Week8.Exercise2 where
import Control.Exception
import Control.Monad.Catch
import Data.Stream.Infinite (Stream (..))
import qualified Data.Tree as Rose (Forest (..), Tree (..))
import qualified Data.Tree.Binary.Inorder as Binary (Tree (..))
import Week8.Exercise1
Exercise 3
Build whatever you want and I will grade it based on how cool it is and how well you managed to utilize what you have learned on this course.
If you are out of ideas, the following modules may provide some inspiration.