Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Monad

Name: Anonymous 2011-07-31 16:15

hey /prog/, could someone explain the concept of a monad to a C programmer?

Name: Anonymous 2011-07-31 18:36

I'll explain using Haskell. If you're talking about category theory or something else, then sorry, I can't help you.

Basically, 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 second
return :: (Monad m) => a -> m a - puts a value in a monad
fail :: (Monad m) => String -> m a - returns from a monadic function

What's more important is how they are actually used. They can be used to represent functions with side effects (IO), functions that can fail (Maybe) or just a container for a value ([]). Monads aren't inherently impure - lists are monads, for example.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List