Contact FutureLearn for Support
Skip main navigation
We use cookies to give you a better experience, if that’s ok you can close this message and carry on browsing. For more info read our cookies policy.
We use cookies to give you a better experience. Carry on browsing if you're happy with this, or read our cookies policy for more information.

Skip to 0 minutes and 4 secondsJEREMY: Functional programming languages have a reputation for being complex. This is probably deserved, but it's off-putting to novice programmers. One of the most intimidating features about Haskell is monads. In this video we're going to delve into them in more detail to try and understand what monads are really all about. Firstly, cue some relaxing music.

Skip to 0 minutes and 38 secondsNow let's start off with the following high level definition. Monads allow sequencing of function calls to be enforced by the type system. Is that enough? Well, perhaps it's enough to be getting on with for now. Monads allow computations to be chained together. Effectively, a monad is a computation pattern as Katie from Facebook told us last week. To reassure you further, we've already encountered at least two examples of monads on the course. The IO monad. In week two of the course we studied IO actions like putStrLn and getLine. We looked at the do construct for sequencing these actions, so the result of one IO action could become the input of a subsequent IO action. For example, look at this code.

Skip to 1 minute and 39 secondsThis is a use of the IO monad wrapped up as a do block. Secondly, the maybe monad. In week three of the course we studied maybe values, which could be Just x or Nothing. These are used to represent values from computations that may fail. Again, we can use do blocks to order a sequence of computations on maybe values. This is the maybe monad. Look at this block. What value does it return?

Skip to 2 minutes and 24 secondsNote that the return keyword here put the final computed value in an appropriate context. More details in the next step. Here's another do block.

Skip to 2 minutes and 39 secondsWhat value does it return? The Nothing value propagates through the do block and is returned. We are going to investigate the properties of monads over the next few steps on the course to try and understand them in some more detail. However, I hope it's moderately reassuring to realize that we've already encountered monads in Haskell and understood them at some level.

We Already Know About Monads

Monads, as a concept, cause trouble to some novice Haskell programmers. How and why do we need to understand this abstract mathematical formalism?

Over the next few steps, we are going to explore monads in some detail. For now, we want to reassure you that we have already used monads — for input/output and Maybe values.

Share this video:

This video is from the free online course:

Functional Programming in Haskell: Supercharge Your Coding

University of Glasgow

Course highlights Get a taste of this course before you join:

  • Haskell history poster
    Brief History of Haskell
    article

    What are the origins of the Haskell programming language? In this article, Dr Jeremy Singer explores the history of Haskell.