More example if you wish. With some more operators.
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.
How does Monad works
I found good explanation about these three magic words (functor, applicative, and monad), using boxing analogy. I really respect the reader on how he made everything clear by putting some funny image on his article.
There’s a caveat in boxing analogy.
I did this note based on
But since, it has some differences, with some additional notes also.
I decide to make my own article.
Context: Maybe, Just and Nothing
Haskell has context that can be wrapped around data type.
This context approach solved common null value issue.
Now a data Maybe contain Nothing or Just a value.
GHCI and do some Math.
Compare with plain data above, now we can wrap data in a context.
Just is just a wrapper. Do not forget the bracket.
Consider this Prelude using application operator and its reverse.
Function is left associative. Apply left bare function to the bare value right.
How do we suppose to do simple thing with data context?
Here comes the
<$> operator in rescue.
<$> is left associative.
Apply left bare function to the wrapped context right.
Lists are functors.
We can rewrite it using
map for list.
fmap is a generalisation of
map is a specialization of
Function are Functor too. It looks like magic, but it is not.
How about also wrapping the left function ?
Here comes the
<*> is also left associative.
Apply left wrapped function to the context right.
Wrapping, in Functor and Applicative
Using boxing analogy.
<$> wrapped the value.
<*> also wrapped the function.
Go further with both.
Apply it to bind.
And chain ‘em.
Rewrite the last sentence in multiline GHCI.
And finally in
Operator Cheat Sheet
Bear in mind
fmapas an infix operator.
(<$>) :: Functor f => (a -> b) -> f a -> f b
(<$>) = fmap
fmap :: Functor f => (a -> b) -> f a -> f b
map :: (a -> b) -> [a] -> [b]
(<*>) :: f (a > b) -> f a -> f b
Bear in mind.
More in this blog.
Meanwhile. Consider go back to
Thank you for Reading.
- July 2017
- June 2017
- Examining Bind in Haskell: Do Notation
- 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