In the
previous article, we learned what state monads were and
how they worked. In this article we'll work through a complete (though very
simple) example and we'll constrast Haskell's state handling with the equivalent
code written in C.
(
Read more )
Comments 16
I wonder if you could digress, just a little bit, into category theory to give some insight into monads as functors and all this lifting business. I understand a functor that lifts a function on some type to lists of that type. I also understand the list monad. But how are those two related?
Reply
Reply
I know the facts about functors and monads, but I'd like to develop better intuitive understanding. For instance, your explanation of operator >>= in terms of function composition was an eye-opener for me. Once I got that, the rest just followed.
BTW, what's a good way of thinking about side effects. Do they, conceptually, happen at the end of the do block? What if you have two different side-effects monads and execute two do blocks, one for each monad. Can their side effects interleave?
Reply
Thanks for the promotion! ;-) I'm really glad you like my approach.
I want to defer a full explanation of your question for my next blog post (about the IO monad) as there are a bunch of subtleties. The critical thing is that all operations occur in the correct sequence. If you have two do blocks in the IO monad, then they are both are executing inside another do block (i.e. in the IO monad, whether or not there is an actual do block); this means that they happen in sequence (no interleaving). If you want to execute an IO action outside of the IO monad there is unsafeInterleaveIO which can indeed interleave IO actions (and bad things can happen if you don't know what you're doing). See, for reference:
http://haskell.org/ghc/docs/6.12.1/html/libraries/base-4.2.0.0/System-IO-Unsafe.html#v:unsafeInterleaveIO
Reply
- Monad transformers. I'm doing some stuff with RandT right now, and if it hadn't been for the folks on beginners-haskell, I never would have figured out how to use it. I've got it working, but I probably only understand 80% of what I'm doing.
- Working with multiple monads at once. For example, I experimented with having a record structure with components that are monads, and the record as a whole was a monad too. I ran into some difficulties with that approach, but I wasn't sure whether that was because of my lack of knowledge, or because what I was trying to do was A Bad Idea.
Reply
Reply
http://article.gmane.org/gmane.comp.lang.haskell.beginners/6055
I got this excellent reply from Brent Yorgey, which solved that problem and showed me a better approach (using an adapter) to accomplish what I needed
http://article.gmane.org/gmane.comp.lang.haskell.beginners/6067
That particular problem is sorted now, but perhaps it gives you an idea of the type of thing that has given me problems. But as I said before, whatever you write next will probably be just what I need to read.
Reply
Reply
Reply
Reply
Reply
Reply
Leave a comment