This repository is my own solutions to the haskell book.
I won't go through every single exercise as I have already read the Learn Yourself Haskell.
Other solutions can be viewed here or find dissucsions in /r/HaskellBook on reddit.
associative a b c =
(a <> b) <> c == a <> (b <> c)
A Monoid is also a Semigroup.
leftIdentity a =
a <> mempty == a
rightIdentity a =
mempty <> a == a
identity f =
fmap id f == f
composition f g x =
fmap g (fmap f x) == fmap (g . f) x
An Applicative is also a Functor.
identity v =
pure id <*> v == v
compositiion u v w =
pure (.) <*> u <*> v <*> w == u <*> v <*> w
homomorphism f x =
pure f <*> pure x = pure (f x)
interchange u y =
u <*> pure y = pure ($ y) <*> u
An Monad is a also an Applicative (is also a Functor.)
rightIdentity m =
m >>= return == m
leftIdentity x f =
return x >>= f == f x
associativity m f g =
((m >>= f) >>= g) == (m >>= (\x -> f x >>= g))
There is no laws for Foldable.
A Traversable is also a Foldable and a Functor.
import Data.Functor.Identity
import Data.Functor.Compose
naturality t f =
t . traverse f == traverse (t . f)
identity x =
traverse Identity x == Identity x
composition x =
(sequenceA . fmap Compose $ x) == (Compose . fmap sequenceA . sequenceA $ x)
-- It's a type which can be reduced from
Monad m => m (t (m b))
-- to
Monad m => m (m b)
Monad tranfromers usually use T
to represent itself. For example, IdentityT
is an monad transformer and the T
represents the following in structure Monad m => m (t (m b))
-- IdentityT :: f a -> IdentityT f a
Monad m =>
m (T m b)
-> m (m b)
-> m b
-> T m b
This is useful when you have nested monads but you want to move the T
to the outermost (aka base type) position.