

Monads in pictures - chrislo
http://newartisans.com/2012/08/monads-in-pictures/

======
tikhonj
That's not really how I visualize functors. Let's go back to the function
picture:

    
    
        ---          ---
        |a| -- f --> |b|
        ---          ---
    

This is fairly simple--using f, you can go from a to b.

Now lets imagine a functor F (with f still being our function):

    
    
        ---          ---
        |a| -- f --> |b|
        ---          ---
              | |
              |F|
              \ /
               V
        ----           ----
        |a'| -- f' --> |b'|
        ----           ----
    

So a functor is just a function at a higher level. A function maps values of
one type to values of another type. A functor maps types to other types. In
Haskell, types in the Functor class always map types of kind * back onto * ,
so they're much like functions of type a -> a.

The crucial part to notice is that it not only maps between types but it also
_preserves_ functions. That is, you get new types corresponding to your old
types and new functions corresponding to your old functions.

So the list type, for example, maps any type a to [a] and any function (a ->
b) to ([a] -> [b]). In a sense, it is like a well-behaved function between
types.

Coincidentally, thinking of functors like this is how I realized that functors
in Haskell are _not_ completely unrelated to functors in OCaml--they're just
different reifications of the same mathematical idea.

~~~
silentOpen
Unrhetorically,

What is the relationship between _fold + cons (::)_ and monadic _bind_? What
is the natural transformation from fold to functorial _map_? Is there a
sensical dual?

Thanks.

~~~
johnpmayer
> What is the relationship between fold + cons (::) and monadic bind? What is
> the natural transformation from fold to functorial map? Is there a sensical
> dual?

Whew, working backwards.

Fold is like your swiss army knife for working with entire lists at once; you
just need to tell it what to do when it sees a node (Cons) or a terminator
(Nil). If, for example, you instruct fold to apply a function to each element
of the list but leave the linked structure intact, you've implemented map! And
for lists, the fuctorial map, or fmap, is exactly map.

The monadic bind in the List monad is a way of expressing Nondeterminism.
Let's say I have two functions...

    
    
      b = makeSomeBsFromAnA(a); c = makeSomeCsFromAB(b);
    

In the context of the List Monad, you would evaluate the first statement to
produce a list of Bs. Then, for each B, you would generate a list of Cs, and
then concatenate all of those lists into a big list. That's why _bind_ is
sometimes referred to as _flatmap_ in the List Monad - take each element
generate in the last list computation to generate a bunch of new lists, and
then flatten them (smoosh them together; concatenate them). _flatmap_ could of
course be implemented using fold and cons.

The point here is that, since we're working within the context of the list
monad, I can deal with a single B and a single C, even though the function
calls are really giving back lists of those respective types.

By the way, in Haskell the code would really look like this

    
    
      makeSomeCsFromAnA a = do
        b <- makeSomeBsFromAnA a
        makeSomeCsFromAB b

------
vytis
Site seems to be down, here's a cached copy:
[http://webcache.googleusercontent.com/search?hl=en&clien...](http://webcache.googleusercontent.com/search?hl=en&client=safari&rls=en&sclient=psy-
ab&q=cache%3Anewartisans.com%2F2012%2F08%2Fmonads-in-
pictures%2F&oq=cache%3Anewartisans.com%2F2012%2F08%2Fmonads-in-
pictures%2F&gs_l=serp.3...60377.63295.1.66800.8.8.0.0.0.1.193.880.0j6.6.0.les%3Bernk_fspiked..0.2...1c.xvj46ZWKaRg&pbx=1)

------
kristopolous
Decent article, but I have something a little off ... it's an honest question
though:

What compels people to use words like "grok"? Is comprehend, digest,
understand, follow, etc. so insufficient that it necessitates an ugly sounding
word meaning emotional absorption to be misappropriated as mental absorption?
There's no good tense rules established and it's not widely known outside the
domain of nerdom. So I ask, to what ends?

~~~
boothead
Here you go <http://en.wikipedia.org/wiki/Grok>

~~~
dllthomas
Also <http://www.catb.org/jargon/html/G/grok.html>

------
Lewton
I know nothing about Monads, I know nothing about Haskell except for the fact
that it's a functional programming language that has Monads. Yet I still found
this really informative... Makes me want to play around with Haskell and see
if this article makes it easy to grok how Monads work

------
jfaucett
from a non-Haskell programmer here. It looks like monads are just a way to
call functions in difference context, in js

    
    
      map(myObj, someFunc, contextObj)
    

Or am I missing something here?

~~~
johnpmayer
It's a bit more powerful than that - consider what would happen if, every time
you sequence two statements together...

    
    
      doThingOne(); doThingTwo()
    

... you get to pick what happens _during the semicolon_ depending on which
Monad you happen to be working with.

For example, like praptak mentioned, maybe during every semicolon you can
evaluate the failure state of your program and decide not to continue. I.E.,
"Maybe" the execution worked, and "Maybe" I have a valid computation at this
point, but "Maybe" not!

The power here is that while you are really operating in this context with an
implicit possibility of having failure, you can program _as if each step
succeeded_. So now we can string together a whole sequence of possibly-failing
computations, and create a bigger possibly-failing super-computation that
error checks under the hood.

~~~
jfaucett
ok, gotcha that is a pretty nifty feature :)

------
olh
Can someone point me more resources for category theory in the computer
science point of view?

------
bestest
Someone's not ready for the HN-Effect? Website not responding.

