[L03 Moonads] Implementing monad instances and library functions

46 views
Skip to first unread message

David Tchepak

unread,
Jan 31, 2013, 7:40:29 PM1/31/13
to haskell-exercises
Next exercise is Moonads.hs:

https://github.com/tonymorris/course/blob/master/src/L03/Moonad.hs

Tests can be run from GHCi in the test/ directory:

>> L03.Moonad.Tests.main


## For new Haskellers

I found this one really challenging. Please ask for help if you get
stuck. For much of this exercise, if you get it compiling then you
probably have the right answer.

First up we need to implement `fmaap'` in terms of `bind` and
`reeturn`, which is a matter of finding out how to fit the types
together. We know from last exercise that functors are types with
valid `fmap` implementations, so this is telling us that all monads
are also functors (as we can implement `fmap` using `bind` and
`return`).

Next we implement `bind` and `reeturn` for the types we've been
working with: List, Id, Optional. `return` will produce a new value of
a particular type, given a single value (reeturn :: a -> m a). So
`reeturn 2` for list will produce a single element list containing 2.
(i.e. 2 :| Nil, or [2]). The `bind` function needs to apply a function
`a -> m b` inside an `m a`, and produce the resulting `m b`.

Finally, we implement some general functions for all monads, i.e.
types that support `bind` and `reeturn`. This means we are pretty much
restricted to calling `bind`, `reeturn` and `fmaap'` to construct
these functions (as well as some general prelude functions like
`foldr`, `const`, `id` and `replicate`).


Good luck!
Reply all
Reply to author
Forward
0 new messages