• Home
  • Textbooks
  • Programming in Haskell
  • Monads and more

Programming in Haskell

Graham Hutton

Chapter 12

Monads and more - all with Video Answers

Educators


Chapter Questions

Problem 1

Define an instance of the Functor class for the following type of binary trees that have data in their nodes:
data Tree a = Leaf $\mid$ Node (Tree a) a (Tree a)
deriving Show

Check back soon!

Problem 2

Complete the following instance declaration to make the partially-applied function type $(a \rightarrow)$ into a functor:
instance Functor $((->)$ a) where
Hint: first write down the type of fmap, and then think if you already know a library function that has this type.

Check back soon!

Problem 3

Define an instance of the Applicative class for the type $(a->)$. If you are familiar with combinatory logic, you might recognise pure and $\langle\star\rangle$ for this type as being the well-known $K$ and $S$ combinators.

Check back soon!

Problem 4

There may be more than one way to make a parameterised type into an applicative functor. For example, the library control.Applicative provides an alternative 'zippy' instance for lists, in which the function pure makes an infinite list of copies of its argument, and the operator $\langle\star\rangle$ applies each argument function to the corresponding argument value at the same position. Complete the following declarations that implement this idea:
newtype ziplist a $=\mathrm{z}$ [a] deriving Show
instance Eunctor zipList where
- fmap :: (a $\rightarrow$ b) $\rightarrow$ Ziplist a $\rightarrow$ Ziplist b
$\operatorname{mip} g(z \times s)=\ldots$
instance Applicative ziplist where
- pure: : a $\rightarrow$ ziplist a
pure $\mathrm{x}=\ldots$
$-\langle *\rangle:$ : ziplist $(\mathrm{a} \rightarrow \mathrm{b})->$ ziplist a $\rightarrow$ ziplist b
$\left(\begin{array}{ll}2 & g s\end{array}\right)<\$>\left(\begin{array}{ll}Z & x s\end{array}\right)=\ldots$
The ziptist wrapper around the list type is required because each type can only have at most one instance declaration for a given class.

Check back soon!
02:33

Problem 5

Work out the types for the variables in the four applicative laws.

Raushan Kumar
Raushan Kumar
Numerade Educator

Problem 6

Define an instance of the monad class for the type (a $>$.

Check back soon!

Problem 7

Given the following type of expressions
data Expr a = Var a | Val Int | Add
(Expr a) (Expr a)
deriving Show
data Expr a = Var a $\mid$ Val Int $\mid$ Add
(Expr a) (Expr a)
deriving show
that contain variables of some type a, show how to make this type into instances of the Functor, Applicative and Monad classes. With the aid of an example, explain what the $\gg>=$ operator for this type does.

Check back soon!

Problem 8

Rather than making a parameterised type into instances of the Functor, Applicative and Monad classes in this order, in practice it is sometimes simpler to define the functor and applicative instances in terms of the monad instance, relying on the fact that the order in which declarations are made is not important in Haskell. Complete the missing parts in the following declarations for the ST type using the do notation.
instance Functor ST where
- fmap :: (a $\rightarrow$ b) $\rightarrow$ ST a $\rightarrow$ ST b
fmap $g$ st $=$ do $\ldots$
instance Applicative ST where
- pure :: a $\rightarrow$ ST a
pure $x=s(\backslash s \rightarrow(x, s))$
$-(\langle *\rangle):$ ST $(\mathrm{a} \rightarrow$ b) $\rightarrow$ ST a $\rightarrow$ ST b
stf $\langle *\rangle \operatorname{stx}=$ do ...
instance Monad ST where
$-(\gg=)::$ ST a $\rightarrow$ (a $\rightarrow$ ST b) $\rightarrow$ ST b
$s t>=f=S(\backslash s \rightarrow$
let $\left(x, s^{\prime}\right)=\operatorname{app}$ st $s$ in app (f $x$ ) $s^{\prime}$ )
Solutions to exercises 1-4 are given in appendix A.

Check back soon!