Monads in functional programming
|
In computer science, monads are used to express sequential composition in lazy functional languages (see also functional programming paradigm). Essentially, they provide the ability to order commands in a temporal sequence, such that later commands can use the results of earlier commands.
A monad is a way to structure computations in terms of values and sequences of computations using those values, thus allowing the programmer to build computations using sequential building blocks (which can themselves be sequences of computations). The monad specifies how combined computations form a new computation, and the programmer is freed from repeating previous combinations.
In Haskell, monads are used to incorporate the I/O system into the language in a purely functional way. Monads are also used for many other purposes, such as parsing (with Parsec), implicitly passing around state (with the State monad), and emulating backtracking (with the List monad).
One advantage of monads is that they don't allow you to mix different types of computations together; to do that would result in a type error. But sometimes, this is desired behavior, so monad transformers have to be used. This can get awkward, though.
External links
- Monads for the Working Haskell Programmer (http://www.engr.mun.ca/~theo/Misc/haskell_and_monads.htm)
- All about monads (http://nomaware.com/monads/html/index.html)