Name: Anonymous 2011-07-31 16:15
hey /prog/, could someone explain the concept of a monad to a C programmer?
Monad is just a typeclass. That means that it has to implement a few functions:(>>=) :: (Monad m) => m a -> (a -> m b) -> m b - takes a value out of a monad and passes it to a function, which returns a monad(>>) :: (Monad m) => m a -> m b -> m b - evaluates the first argument, throws away the result, and evaluates and returns the secondreturn :: (Monad m) => a -> m a - puts a value in a monadfail :: (Monad m) => String -> m a - returns from a monadic functionIO), functions that can fail (Maybe) or just a container for a value ([]). Monads aren't inherently impure - lists are monads, for example.
return x >>= f == f x
x >>= return == x
x >>= (\y -> f y >>= g) == (x >>= f) >>= g
return is (lambda (x) (list x)), and >>= is (lambda (x f) (append-map f x)).Monad typeclass just encapsulates that concept. You never ``need'' them.