
On Wed, Dec 21, 2005 at 11:43:42AM +0000, Daniel Carrera wrote:
Hi all,
I'm a Haskell newbie and I don't really understand how Haskell deals with functions that really must have side-effects. Like a rand() function or getLine().
I know this has something to do with monads, but I don't really understand monads yet. Is there someone who might explain this in newbie terms? I don't need to understand the whole thing, I don't need a rand() function right this minute. I just want to understand how Haskell separates purely functional code from non-functional code (I understand that a rand() function is inevitably not functional code, right?)
You said you were a mathematician, so let me give you the mathematical version in case you like category theory: A monad is an monoid object in the category of endofunctors. That is, a monad is an endofunctor 'F' (i.e., a data type depending on a single parameter, which is furthermore an instance of Functor), with a natural transformations from 'F (F a)' to 'F a' and from 'a' to 'F a', satisfying the usual rules for identity and associativity. Exercise: List is a monad. So is Maybe. You can think about a monad 'F a' as a computation of an object of type 'a' with side effects; for instance, the List monad allows the side-effect of multiple answers (like non-determinism), and the Maybe monad allows the side-effect of failure. One particularly useful and complicated monad is the IO monad, which allows interactions with the outside world. Peace, Dylan