# Motivation

If you’re a programmer you are likely already using Monads quiet frequently. One popular example is Future in Java or Promise in JavaScript. You probably have seen or written code like fetch(url).then(do this).then(do that) or you’ve used the async/await syntax. You might have noticed that you cannot get a value out of the Promise. As soon as you write await your function has to be async (has to be a Promise itself). Or when you use .then() you cannot get the value out of the then. This is because the value only exist after your Promise (the computation) has be resolved, e.g. an API call has been made and the result has arrived.

# Applicative

Next thing we are going to implement is Applicative. The interface for Applicative in Haskell looks as follows:

We have to implement two functions. The first one is very simple. Given a type a lets say Integer, we simply have to put it into our structure, e.g. Int -> [Int]. And the second function states, that given two Applicative, e.g. two lists, and one containing a list with a function, we simply apply the functions of that list to every element of the other list.

This is very similar to the implementation of Applicative for lists in Haskell fs <*> xs = [f x | f <- fs, x <- xs] and [(++"0")] <*> ([(show)] <*> ([(+ 1)] <*> [1,2,3])) yields the same result.

The Monad interface requires us to implement two functions. The return is very similar to pure, we just have to lift a value into the Monad, e.g. put a value into a list. The second function >>= also called bind, takes a function that converts an a to m b e.g. from a -> [b] in case of lists.
As we can see the implementation is very similar, the bind function consists of concat and map, also called flatmap. And this is how we can use the monadic bind function in Haskell [1,2,3] >>= \x -> [(x+1)] >>= \x -> [(show x)] >>= \x -> [x ++ "0"]. Here you can find the full implementation of Functor, Applicative and Monad for list in Python: