While in examining binding article, we start from bind operator, then convert it to do notation. This article start from do, and revert it back to monadic code using a few operator.
I also add Kleiski Fish Operator, that is very useful as a shortcut in a do notation.
This tutorial/ guidance/ article is one of some parts.
References: About Monad.
Examining Bind: Bind
>>=operator. Hello World Example.
<$>operators. Personal Notes. Example using Number.
Monadic Operator: Fish
The first one is overview, then some references. The last three parts is all about Example Code.
I you need to know more about operator,
you can just ask
And if you are curious for another operator, you can read this article and have fun.
then >> operator
bind >>= operator can be found here.
Think Function in Haskell as Math Equation
Suppose we have this very short function.
Think Action in Haskell as Procedure, Sequence of Command
We can rewrite this as an action
do special notation,
we can desugar the
then >> operator as below.
Writing it oneliner, would make this action looks exactly like a function. And in fact, it is just a function.
then >> operator is
bind >>= operator,
except it ignore input.
We need other example containing input.
do notation does avoid coding horror,
but sooner or later we need to know what is inside.
Consider this action.
Each command has a
Maybe String result type.
We can desugar the action above
into vanilla monadic code without
Or you can make it oneliner vanilla monadic code.
Here is another IO example, showing you home directory.
There is also good example here
Operation inside Bind
Monads is way to unwrap stuff, do something about it, and wrap the result. Or better, Monad give access for method, to work inside wrapped stuff, without unwrapping it.
This list, will show Monad is overloaded for different types. Every monad has its own implementation.
This will display
It can be desugared into oneliner vanilla monadic code.
The Monad Class
It is clear that sequence command in do notation is
just functions chained together
bind >>= operator
then >> operator.
Now the next question. What is these two operators has to do with monad ? So here it is the definition of monad in Prelude 220.127.116.11
Those two are Monad Operators.
What other practical used, Monads are good for?
There are many I guess. One of them is Kleiski Arrow.
Kleiski Arrow does function composition, just like
except it perform monadic effects.
Reference about Kleiski Arrow
<=< operator can be found here.
I looks like fish, so we can call this
Consider this function
We can avoid closing bracket
function application $.
Or we can join two function
function composition ..
Consider this action
Since the output is
IO String, while expected input type
The composition have to be Monadic. Kleiski Arrow perform this well.
Or Kleisli Composition, using reversed arrow.
Thank you for Reading.
- July 2017
- June 2017
- Examining Bind in Haskell: Example using Number
- Examining Bind in Haskell: Hello World
- 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