DEV Community

loading...

A Good Video for Haskell Monad

chenge
Ruby, and learn Rust, Go, Elixir, Erlang...
・1 min read

A good video although I don't understand monad now.

Discussion (6)

Collapse
zeljkobekcic profile image
TheRealZeljko

The video series of Abbas A on youtube, the (online) book "Learn you a haskell" and the wikipedia article on monads helped me a lot!

If could try to explain you monads the way I did if you want.

Collapse
chenge profile image
chenge Author

wait your post.

Collapse
zeljkobekcic profile image
TheRealZeljko • Edited

Okay, a monad is in the first place nothing more than a "data structure" and a definition of some rules. In Haskell you can then do non-pure and non-deterministic stuff easier.

Let us take a look at a data structure and the >>= (we call it bind) operator. The bind takes to following arguments: Something wrapped in a data structure (I will try to avoid the word monad) and a function.

For example this data structure can be a list with elements of one type (in Haskell you would say [a]). The bind function takes a function with the following type-signature a -> M b. Let's make the M b clearer. The M b just says it returns an element of type b wrapped in a Monad (or Datatype like a list, Maybe, Either and so on)

For lists the bind operator is flatMap (in Haskell it is concatMap. Here is an example:

Prelude> a = [1,2,3]
Prelude> concatMap (\x -> [x, x]) a
[1,1,2,2,3,3]
Prelude> a >>= (\x -> [x, x])
[1,1,2,2,3,3]

The next thing you need to provide is a function which you can give an element and it returns this element wrapped in the data structure. The name for this function is not so well chosen, it is called return. That is it. This is an example for the list data structure.

Again: A monad is a data structure which just provides atleast two functions. One is called bind and must have a specific type signature and the other is called "return" and just wrapps an element into a monad (data structure).

This is just a simple example and should help you to understand it a little. Let's continue with the IO Monad. Let's say you want to read something from the command line. One way to do this is to use the getLine function. It returns an IO String. The IO tells us that it is a monad for Input and Output and inside this "data structure" is a string. Nice. The bind operator for the IO monad just takes the element inside it out, applies the function to it and then wraps it back into an IO monad. Let's see that in action:

Prelude> getLine >>= (\x -> return $ x ++ x)
hello world
"hello worldhello world"

What we are doing here is: We call getLine and get an IO String monad which holds the input "hello world". Then we call bind on it with a function which just repeats the string. Then we return a IO monad with the modified string.

Now we want to do side effects:

Prelude> getLine >>= (\x -> putStrLn "test" >>= (\_ -> return $ x ++ x))
hello world
test
"hello worldhello world"

Here we first read input from the command line. Then call bind with a larger function to it. But now we have put a putStrLn between the part where we repeat the input. This is the side effect we want to do. This could be a rocket launch, a new tweet from Trump or your reply on dev.to.

Hope this helped you.

Thread Thread
chenge profile image
chenge Author • Edited

Monad means impure. It is used to hint this is impure to divide pure and impure. Is this right? Thanks for your great explain.

Thread Thread
zeljkobekcic profile image
TheRealZeljko

I think monad means something different in the cathegory theory (where they originate from) but we do not need to bother with that. In monads you can divide the impure part of your programs from your pure part (in a pure way). So yes, your statement is correct. Glad I could help you out :)

Thread Thread
jvanbruegge profile image
Jan van Brügge

Monad means neither pure nor impure. The Monad is the essence of the idea of sequencing computations. This can be used for side effects, but that is completely orthogonal