REVISED: Sunday, October 13, 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 we will refer to as M, with two operations we will refer to as return and bind.
The operation return takes a value of type a and returns a value of type M a.
The operation bind 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 represent various computational concepts, such as state, sequencing, and exceptions.
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 many 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 states.
3. EXAMPLE
Here is an example of how to use the Maybe monad to represent a computation that can fail:
-- mayMon.hs
import Control.Monad()
{-
The function divide x y returns a Maybe Integer, representing the result of dividing two numbers. x is the numerator, y is the denominator.
-}
divide :: Integer -> Integer -> Maybe Integer
divide x y =
if y == 0 then Nothing else Just (x `div` y)
{-
The function main uses the `Maybe` monad to print the result of dividing x by y.
-}
main :: IO ()
main = do
xStr <- readLn
yStr <- readLn
result <- divide x y
case result of
Just z -> print z
Nothing -> putStrLn "Division by zero!"
This program effectively handles division by zero using the
Maybe
monad, providing a Nothing
value in such cases and printing an appropriate error message.Here's a breakdown of the code:
import Control.Monad()
: Imports theControl.Monad
module, which provides essential functions and type classes for working with monads, includingMaybe
.divide :: Integer -> Integer -> Maybe Integer
: Defines a function nameddivide
that takes twoInteger
arguments (numerator and denominator) and returns aMaybe Integer
quotient result.if y == 0 then Nothing else Just (x
divy)
: Uses a conditional expression to check if the denominator (y
) is zero. If it is, the function returnsNothing
to indicate an error (division by zero). Otherwise, it returnsJust (x
divy)
, wherex
divy
performs integer division andJust
wraps the result in aMaybe
value.main :: IO ()
: Defines the main function that executes the program's logic.x <- readLn
: Reads anInteger
value from the standard input and binds it to the variablex
.y <- readLn
: Reads anotherInteger
value from the standard input and binds it to the variabley
.result <- divide x y
: Calls thedivide
function with the values ofx
andy
, binds the result (eitherJust z
orNothing
) to the variableresult
.case result of
: Uses a pattern matchingcase
expression to handle the different possible values ofresult
.Just z -> print z
: Ifresult
isJust z
, it prints the value ofz
(the result of the division).Nothing -> putStrLn "Division by zero!"
: Ifresult
isNothing
, it prints the error message "Division by zero!".
This program effectively demonstrates the use of the Maybe
monad for handling potential errors and providing meaningful feedback to the user.
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.