[Haskell-cafe] Intuition to understand ...
chriswarbo at googlemail.com
Tue Jul 29 09:09:44 UTC 2014
martin <martin.drautzburg at web.de> writes:
> Let's forget about this particular monad for a while and focus on
> intuition in general.
> I managed to dissect the continuation monad and write the bind
> operator correctly. But it took me hours and the train of thought is
> very elusive. I wouldn't call this "understanding".
> When I truly understand things I can come up with analogies, counter
> examples and the like. I can draw diagrams to visualize the thing. I
> can embed the new thing into a world of old things.
> But I cannot do any of these things with the Continuation Monad. Right
> now I am trying to get there by staring at it, asking myself
> questions, trying to write bind in different ways - hoping that one
> day it will click.
> So I wonder what you guys do to develop intuition about tricky haskell
> things, such as the Continuation monad.
Bind can often be a bit tricky. I usually find it easier to define bind
> x >>= f = join (fmap f x)
Then ignore it and focus on fmap and join instead. This is a 'divide and
> fmap :: (Functor f) => (a -> b) -> f a -> f b
This is the usual Functor method. We can ignore all the monadic stuff
when defining it, which makes our life easier.
With fmap defined, we can turn "f :: a -> m b" and "x :: m a" into
"fmap f x :: m (m b)"
> join :: (Monad m) => m (m a) -> m a
This needs to turn two nested monadic "wrappers" into one, using some
logic specific to our monad. I find this easier to think about than bind
since we're now only dealing with one argument.
More information about the Haskell-Cafe