Monads in a simple way
Tags: haskell, functional, programming
Published: 2021-12-13 21:17:15

Monads were created by mathematicians in 1960 and rediscovered by computer scientists in 1990 as a new way to handle effects. Let's write some examples, imagine a function that gets the tail from a list like this:

![tail](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4afbavgitry2ikw13re8.png)

The problem here is obvious, this function can fail if he receives an empty list, now imagine that for any reason we're using this function in our program to work with lists and for some reason, it receives an empty list, in this case, our program will break and the language compiler will give to us an exception, so how to solve this? to solve this we can use the Maybe Monad, let's write this:

![safeTail](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q8ppatpvg4lbdu0yrrpl.png)

Now we have the function **_safeTail_** which is total, which means that this function will never fail, even if receives an empty list. As you can see Maybe Monad is like a box that encapsulates a value, so this box can have some value or nothing, let's write our own Maybe Monad in Haskell:

![Maybe Monad](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fd4jo1hiaw2qpmr0680p.png)

As you can see the Maybe Monad allows us to handle functions that can fail, but now that these values are encapsulated how we can work with him? Monads essentially are Functors so we can map functions in our Monads without changing your structure, let's have an example with lists:

![fmap in a list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mmm5yr9hovxx03g29j4r.png)

Here we map the function **_(+1)_** in our list. As you can see we don’t change its structure, and that’s basically what Functors allow us to do. But, what if we want to pass our boxed value to some function that takes just a value and transforms this into another type of monadic value? We can use the bind operator for this, which is one of the functions necessary for the implementation of a monad. In Haskell, this is represented by the symbol: **_>>=_**.

Imagine that we are using our **_safeTail_** that we write before and now we want to pass the list to a function that takes some list and find the first even element in the list, let's write this function:

![getFirstEven](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wpy5zfiy815e2rdub2yt.png)

After writing this we can pass our element to this function using the bind operator, let's write this:

![bind examples](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r7iwxjmpqmrhvllqoiq8.png)

One thing that you can notice here is that the **_getFirstEven_** function also returns a Maybe Monad, so what the bind operator does is get the value from the box and put him back in the box after doing something. This helps us to compose our monadic functions handling all the effects that we can find on the way.

So the central point here is that Monads allow us to handle effects, like for example handle Input/Output with IO Monad. This is very powerful because it allows us to have handled effects with pure functions, meaning that it also allows us to build real-world applications with pure languages like Haskell.

A little explanation about this: Haskell, for example, is a pure language which means that we just have pure functions. Pure functions are like mathematical functions: they will produce exactly the same output given the same input, so it’s not possible to have side effects, for example. While building a real-world application, on another note, we need to handle side effects like getting some data typed by the user, or reading some file. So we may think that Haskell is a useless language, but Monads allow us to handle these effects while maintaining the functions pure.