Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Wikipedia has a pretty good analogy explanation. I like the idea of monads being "programmable semicolons" which are used to inject side effects between purely functional applications.

So if I understand monads correctly then they are basically functions that work like a Unix pipe between functions which takes input data in a monadic container, performs some side effects (I/O for instance), and yields another monadic container with output data that is forwarded to the next function - all that without violating the purely functional character of the whole application chain.

https://en.wikipedia.org/wiki/Monads_in_functional_programmi...



> performs some side effects (I/O for instance),

What you're describing is the IO monad to first approximation, most monads don't perform side-effects at all ( List, Cont, Maybe, ... ) and the bind operations only performs some pure overloaded operation specific to the monad instance. For example the list monad performs the concatMap function which is pure.

    instance Monad [] where
      m >>= f        = concat (map f m)
      return x       = [x]


"they are basically functions that work like a Unix pipe between functions which takes input data in a monadic container, performs some side effects (I/O for instance), and yields another monadic container with output data that is forwarded to the next function - all that without violating the purely functional character of the whole application chain."

That much is true of any applicative functor (which is a superset of monads). The additional power monad gives you is to change up the later portions of that chain based on the earlier results.

Of course, a lot of uses of "monads" don't really make the distinction (and applicative functors are plenty useful).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: