REVISED: Wednesday, January 24, 2024
1. INTRODUCTION
2. OVERVIEW
Davie, A. (1992). Introduction to Functional Programming Systems Using Haskell. Cambridge, England: Cambridge University Press.
Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.
Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.
What is a monad?
A monad is a type constructor M that comes with two operations:
return: This operation takes a value of type a and returns a value of type M a.
bind: This operation takes a value of type M a and a function of type a -> M b, and returns a value of type M b.
The return operation is used to create new monadic values, and the bind operation is used to chain together monadic computations.
Why use monads?
Monads are a powerful tool for structuring computations. They can be used to represent a wide variety of computational concepts, such as:
State: Monads can be used to represent computations that have side effects, such as reading or writing to a file.
Sequencing: Monads can be used to represent computations that need to be executed in a specific order.
Exceptions: Monads can be used to represent computations that can fail.
Haskell has a number of built-in monads, including:
Maybe: This monad represents computations that can fail.
IO: This monad represents computations that have side effects.
State: This monad represents computations that have mutable state.
3. EXAMPLE
Here is an example of how to use the Maybe monad to represent a computation that can fail:
import Control.Monad
{-
This function returns a Maybe Integer, representing the result of dividing two numbers.
-}
divide :: Integer -> Integer -> Maybe Integer
divide x y =
if y == 0 then Nothing else Just (x `div` y)
{-
This function uses the `Maybe` monad to print the result of dividing two numbers.
-}
main :: IO ()
main = do
x <- readLn
y <- readLn
result <- divide x y
case result of
Just z -> print z
Nothing -> putStrLn "Division by zero!"
4. CONCLUSION
Monads are a powerful tool for structuring computations in Haskell. They can be used to represent a wide variety of computational concepts, such as state, sequencing, and exceptions.
This tutorial has helped you to learn how to program using Haskell monads.
5. REFERENCES
Bird, R. (2015). Thinking Functionally with Haskell. Cambridge, England: Cambridge University Press.
Davie, A. (1992). Introduction to Functional Programming Systems Using Haskell. Cambridge, England: Cambridge University Press.
Goerzen, J. & O'Sullivan, B. & Stewart, D. (2008). Real World Haskell. Sebastopol, CA: O'Reilly Media, Inc.
Hutton, G. (2007). Programming in Haskell. New York: Cambridge University Press.
Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.
Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.