Skip main navigation

New offer! Get 30% off your first 2 months of Unlimited Monthly. Start your subscription for just £35.99 £24.99. New subscribers only T&Cs apply

Find out more

We Already Know About Monads

Novice Haskell developers feel intimidated by monads. In this video Jeremy Singer explains that we are already familiar with two monads.
JEREMY: 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.
Now 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.
This 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?
Note 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.
What 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.

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.

This article is from the free online

Functional Programming in Haskell: Supercharge Your Coding

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now