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 main navigation

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.
4.3
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.
38.3
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.
98.8
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?
143.9
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.
158.6
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

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education