
The Monad Challenges: Jump start your understanding of monads - mightybyte
http://mightybyte.github.io/monad-challenges/
======
LukeHoersten
Doug Beardsley (mightybyte) is a very prominent Haskeller. A maintainer of the
Snap Web Framework for Haskell and Co-organizer of the NYC Haskell group.
Definitely worth reading his stuff. Great tutorial as well!

------
dreamdu5t
Cool tutorials. I think it's much more effective to start with learning
functors and applicatives before monads. I'm surprised at the number of
tutorials aimed at teaching people monads who don't yet understand the more
basic type classes.

~~~
Smaug123
Coming at this from a mathematician's perspective, it's complete madness to
learn what a monad is before learning what a natural transformation (and
therefore a functor) is. I would also consider it somewhat eccentric to do
monads without learning what an adjunction is, since adjunctions are basically
why we're interested in monads in the first place.

~~~
throwupper247
Having been mildly disappointed by the simplicity of monoids, after
intimidation from the complexity in maths, i expect that something like an
adjunction is something already known to a commoner in any case, just that the
vocabulary, particaularies and relations to the concept in question are not
always obvious.

~~~
Smaug123
Monoids really are simple things. Monads are a bit less so, but they're still
fairly simple objects (as evidenced by the fact that they have a short
description, "monoids in the category of endofunctors", even if most people
don't know what that means).

Adjunctions turn up all over maths, but I've been trying for a while to come
up with an example which programmers (as opposed to mathematicians) would
quickly understand. Broadly speaking, they represent "the leanest way to add a
particular structure to something", but of course that's pretty useless for
understanding them!

------
mrcactu5
I found a math background is a disadvantage for learning Haskell, since most
mathematicians _don 't_ really use a lot of category theory. Lately that is
changing, [http://math.stackexchange.com/questions/1617592/is-set-
prime...](http://math.stackexchange.com/questions/1617592/is-set-prime-with-
respect-to-the-cartesian-product?lq=1) There's a lot of confusion on how the
math version of "functor" or "monad" maps to the CS definitions.

I found this tutorial on Monads and Applicatives helpful
[http://adit.io/posts/2013-04-17-functors,_applicatives,_and_...](http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html)

~~~
LukeHoersten
Something that doesn't help the Math->CS understanding of Functors is, for
example, that languages like C++ use "Functor" to mean other things (function
pointer in this case).

~~~
zmmmmm
I'm curious to know which came first?

In any case it's annoying to have different languages adopting the same word
to mean vaguely related but actually completely different things. I suspect
that is why a lot of tutorials about Monads skip talking about Functors
altogether which ends up making the whole thing more confusing.

~~~
archgoon
> I'm curious to know which came first?

The math definitions. These were invented by Saunders Mac Lane and Samuel
Eilenberg back in 1945ish, about a year before ENIAC was built.

------
yyhhsj0521
Yeah so here comes another monads tutorial.

People always say that monads are not that hard and you don't need to
understand category theory to understand monads, but since people still keep
writing tutorials for them (and thus imply that we have yet to see a perfect
one that clearly explains everything you need to know about monad), I
sometimes wonder that if beginners should just start from category theory.

~~~
OscarCunningham
I know loads of category theory, but when I tried to find out how monads were
used in computer programming I was disappointing to find that no one has
written a "Monads in Haskell for Category Theorists"tutorial. :-(

------
ridiculous_fish
I have Haskell experience and would love to do these exercises, but I am
struggling with the environment.

For example, Set1 tells me to make a function fiveRands and then "check my
answers;" how do I do that? I know enough Haskell to do something like:

    
    
        main = putStrLn $ show fiveRands
    

but putStrLn is not part of MCPrelude so this doesn't compile, and the
instructions say explicitly "Do not import any other modules."

I'm excited to try these. Please add some more hand-holding to get me and
others over the tooling and environment issues.

~~~
mightybyte
Hi, I was assuming that people would do their playing around and answer
checking in ghci. MCPrelude does not export putStrLn because it is an IO
function, which is a monad and I specifically wanted to hide all the prelude's
monad stuff. I suggest loading your file in ghci with "ghci Set1.hs". Then you
can just type "fiveRands" at the ghci prompt and it will show you the values.

~~~
ridiculous_fish
Thanks! It would be great to walk through this, in the same way that it shows
the shell commands for installation under "Getting Started."

I hardly ever use the repl because I prefer to keep my tests around. I'm
sheepish to admit this, but I only just now learned that you can pass a file
to ghci.

------
meshuggah
Great. I was wondering if anyone is interested in writing a book or a blog
titled something to the effect of "all the brutal concepts in the languages
you want to learn or respect." For example, monads in Haskell, macros and
continuations in Scheme...Maybe languages like C, Forth, Prolog, Smalltalk etc
also have such arcane concepts that "scare" people??

~~~
pklausler
I don't know about scaring people, but many programming languages have
concepts that are best demonstrated with small examples that make people say
"whoa".

For example, it's amazing that append() in Prolog can be run "backwards" from
the concatenated result to yield all the lists that can be concatenated to
produce it. Or that the monadic bind operator in Haskell (>>=) can be defined
in terms of join and fmap, or that ($) = id.

Programming languages have idiomatic expressions, and learning _why_ they do
what they do can produce enlightenment. It's no accident that C supports
syntax like 3["hello"]. It looks mysterious, but not when you know that x[y]
== *((x)+(y)) by definition. APL was especially rich in idiomatic expressions;
I suppose that it's a sign of having easily composed primitives.

~~~
Patient0

        ($) = id !?
    

< tries it out in Raskell/Ghci >

I see now why it's true, but still... Whoah!! Thanks for that!

I think your suggestion would make a terrific online book.

------
sidcool
How I only wish a similar tutorial for Scala.

~~~
kevinherron
Check out the book "Functional Programming in Scala".

[https://www.manning.com/books/functional-programming-in-
scal...](https://www.manning.com/books/functional-programming-in-scala)

------
fizixer
> Do I need to know Haskell? Yes.

This is enough for me to reject this blogpost.

No you don't need a specific language to implement any functionality that can
be implemented by a turing-complete language.

I watched Crockford's monad talk [1]. He used javascript. The only problem
with that talk is that he spent about 10 seconds or less explaining what a
monad is (and the rest of the talk on implementation issues and his own
perspective, etc, etc).

I'm still waiting for a good monad discussion without CT or Haskell. (this [2]
and this [3] look promising, both in python)

[1]
[https://www.youtube.com/watch?v=b0EF0VTs9Dc](https://www.youtube.com/watch?v=b0EF0VTs9Dc)

[2] [http://www.valuedlessons.com/2008/01/monads-in-python-
with-n...](http://www.valuedlessons.com/2008/01/monads-in-python-with-nice-
syntax.html#6799472260664409462)

[3] [http://www.dustingetz.com/2012/04/07/dustins-awesome-
monad-t...](http://www.dustingetz.com/2012/04/07/dustins-awesome-monad-
tutorial-for-humans-in-python.html)

~~~
rntz
It's true that you don't need to know Haskell in order to learn about monads,
but Haskell is a perfectly reasonable language in which to write a monad
introduction.

Haskell is good for learning about monads for the same reasons monads tend to
be heavily used in Haskell, but not in other languages. Monads are both part
of Haskell's standard library, and have very useful special syntax (do-
notation). Writing out monadic code explicitly gets to be a huge pain when
using monads in another language. Typeclasses also make it convenient & easy
to write code that's generic over any monad.

~~~
imh
>Writing out monadic code explicitly gets to be a huge pain when using monads
in another language.

Couldn't any other language have a syntax like

    
    
        thing.do(\x -> other thing(x)).do(
        \x -> yet another thing(x)).do(
        a function).do(
        more stuff).do(
        etc)
    

And then replace "\x -> whatever" with the local language's lambda syntax. The
only time you get the painful nested parens is if you want

    
    
        thing.do(
        \x -> f(x).do(
        \y -> g(x,y)))
    

It's not too terribly bad.

~~~
ash
Like JavaScript Promises?

