Appetite
How do I suppose to understand this short code?
This tutorial will guide you step by step,
from Functor <$>
, Applicative <*>
,
Bind >>=
, until do
notation.
Explaining Monad
This tutorial/ guidance/ article is one of some parts.

Overview: Summary.

References: About Monad.

Examining Bind: Bind
>>=
operator. Hello World Example. 
Examining Bind:
<*>
and<$>
operators. Personal Notes. Example using Number. 
Monadic Operator: Fish
>=>
operator.
The first one is overview, then some references. The last three parts is all about Example Code.
Preface
I’m not pretending that I know Monad.
I’m staying away from dark arts.
Instead I’m examining behaviour of this almost magic
bind >>=
operator.
Gentle, step by step.
Reference about bind >>=
operator can be found here.
Dotfiles
I’m using GHCI to show each step of this tutorial. I put all the function in one module. This module should be loaded in GHCI.
In module:
In GHCI:
Wrapping and unwrapping
Functor, Applicative and Monad, can be seen as a concept of unwrapping and wrapping data. The box that wrap the data could be IO, list or the simple Maybe context.
These three are standard spells from the book of white wood witch.
This tutorial use String in Maybe context. In order to print, we need to unwrap the context first.
In module:
In GHCI:
Consider a more useful function
In module:
In GHCI:
You don’t need this function in GHCI. But you are going to need this when you do compile in command line.
Simple Data Type: One Argument
Consider a String > String
function.
Start with simple.
In module:
In GHCI:
In GHCI: with Functor <$>
Functor takes any function that can be mapped. Whether Maybe context or a list can be mapped.
Here the data (Just "World")
unwrapped
into "World"
. And the result wrapped back.
We can see that from another perspective. Functor operate inside the list.
In GHCI: with Applicative <*>
ApplicativeFunctor also wrapped the function. So both function and data are wrapped. And the result also wrapped. Here we have two wrapper example, Maybe context as wrapper, and list as wrapper.
I love girl. Treat a girl like a lady.
Simple Data Type with Two Argument
We need this two argument to show how to combine Functor and ApplicativeFunctor.
In module:
In GHCI:
We can greet plainly.
Or Curry the function.
Or even using function application
In GHCI: using functor <$>
And get the wrapped result.
fmap
is just another name
for infix <*>
operator.
In GHCI: with Applicative <*>
Get wrapped, both function and input argument.
In GHCI: with Functor <$> for both arguments
Now the function have a way to operate inside each wrapped argument.
Summary of Functor and Applicative
I hope that you see these patterns clearly.
However, these above are just an example of wrapping and unwrapping.
There is a more complex pattern, that is bind >>=
operator.
Source Code can be found here.
Bind Introduction
Consider our last simple function.
In module:
We can add a failsafe feature using Maybe context as an output.
In GHCI:
Now the output already wrapped. Even with plain call.
Before thinking about chaining, we can rewrite as below.
You can imagine how the bind operator works. Just like ApplicativeFunctor, bind does unwrapping and wrapping operation.
Chaining
Here comes the most interesting part. Monad get it feed from output of sequence. This is what applicative and functor cannot do.
Consider rewrite the function with two arguments, and with Maybe context feature as an output.
In module:
In GHCI:
Here we can chain each function using bind >>=
operator.
Or using the flip version, reverse bind =<<
operator
for convenience.
How does it works ?
The Just "Hello"
unwrapped
into plain String "Hello"
.
And the rest follow.
We have already make the output wrapped in box.
Binding List
In module:
Now consider the Functor version that we already have.
Please pay attention to the function declaration.
In GHCI:
This is not a good example, since it does not apply to numeric. We should move on to the next example.
Bind Example Using Simple List
Now you can have the idea on how the list binded with the Monad operator. By examining the data type in function declaration.
Consider this simple function example.
In module:
In GHCI:
We can map each element in this list easily.
Please pay attention to the function declaration.
Now consider a list function made for bind.
We can rewrite it without map
function.
In module:
Please pay attention to the function declaration.
In GHCI:
We can map each element in this list easily.
How about empty list ?
The if then else
for empty list looks pretty useless in this function.
We are going to have the need for it in the next function.
Or you can just skip it.
List with Two Argument
Consider these two functions with list output.
In module:
Please pay attention to the function declaration.
In GHCI:
We can bind different function together.
Or using flip version operator, as needed, for conveninence, with about the same result.
Summary for Bind
We can examine how bind >>=
can
chain different functions together in such order.
The real world may have more complex case,
not just different functions,
but also functions with different input and output.
Source Code can be found here.
Sequencing
By chaining different function together in such order, we have already make a sequence. One step closer to have sequence of command.
Consider our last chain, using vanilla monadic code.
We can rewrite in a function, still with vanilla monadic code as below
Run this in command line, we will have the same result.
Now we can convert it, for use with do
notation.
In every command inside do
notation,
each must have the same data type with the function output.
It is the list, [String]
.
Summary for Do notation
do
notation, make sequence of command possible.
Well, I’m still not sure that I have understand Monad
.
But I’m sure that many times I greet the World and Lady.
I like the World. I like a Girl.
Source Code can be found here.
Thank you for Reading.
Archives
 July 2017
 June 2017

May 2017
 Examining Bind in Haskell: Do Notation
 Examining Bind in Haskell: Example using Number
 Explaining Monad: References
 Explaining Monad: Overview
 Loop in Haskell With Map, Part Three
 Loop in Haskell With Map, Part Two
 Loop in Haskell With Map, Part One
 Loop in Haskell With Map, Overview
 Modularized HerbstluftWM in Haskell
 Modularized HerbstluftWM in Lua
 Modularized HerbstluftWM in PHP
 Modularized HerbstluftWM in Ruby
 Modularized HerbstluftWM in Python
 Modularized HerbstluftWM in Perl
 Modularized HerbstluftWM in BASH
 Modularized HerbstluftWM Overview
 April 2017