
What I Wish I Knew When Learning Haskell - psibi
http://dev.stephendiehl.com/hask/
======
jrockway
Agree with the advice about monads. You're going to think monads are weird
because the first one you encounter is used to hide a particular detail of how
Haskell works in the real world, namely that Haskell can't mutate objects, but
that running your program mutates the real world. Ignore this use case,
because while interesting, it's a messy implementation detail, not something
to model in your own code.

Use monads for bailing out of computations early (Maybe/Either) or passing
some state along through a computation (State). When you're comfortable with
those concepts, look at ST for a generalization of what IO does. That is
something you can actually use to simplify your own programs.

~~~
mjn
The State monad is what really sold me on the concept. Right before
encountering a tutorial that mentioned it, I had just spent some time writing
some code in Common Lisp, where I was trying to find a nice abstraction to
avoid threading a state variable through absolutely everything. So this came
across as "dear god, the abstraction _does_ exist!" Whereas the IO monad at
first blush comes across as "lol PLs geeks realized they had to actually do
I/O", which is not as compelling, in part because that version comes across as
a solution to a self-inflicted problem.

It's slightly disappointing, though, that State doesn't work that well when
translated back to Lisp:
[http://marijnhaverbeke.nl/monad.html](http://marijnhaverbeke.nl/monad.html)

~~~
jrockway
State is exactly the right abstraction for this. When people using other
languages come up with an "abstraction" for state, it is always becomes a
testing/debugging nightmare. With State, it really _is_ just a parameter to
your function, which you're allowed to hide _if you feel like it_.

------
sillysaurus2
Would someone please explain what a monad is and why they're useful, in down-
to-Earth, simple language that an engineer can appreciate?

I know (and use) basically every other CS concept. Monads, though, I've never
bothered looking into, because it seems like everyone who talks about them
can't resist the urge to use flowery descriptions of their possibilities,
rather than examples of their pragmatism.

Also, it's suspicious that pg has never once mentioned monads:
[https://www.google.com/search?q=site%3Apaulgraham.com%20%22m...](https://www.google.com/search?q=site%3Apaulgraham.com%20%22monad%22)

If they're useful, you'd think one of the best hackers would have said
something about it. He spent a good deal of his career talking about closures,
types, objects, functions, computing theory, lambda calculus, etc. But no
monads.

So, are they useful? Would anyone please give examples of their usefulness?

EDIT: You know what's worse than hero worship? Not being able to make a
reasonable argument which uses someone as an example without being accused of
hero worship.

~~~
Udo
I was glad when the author of the article advised against learning Monads by
analogy, because it's one of those subjects where analogies serve only to
confuse people. Personally, my brain maps badly to Haskell syntax, so let's do
this in JavaScript (I know, I know, not so powerful and everything):

In fact if you used jQuery, you already know what a Monad is, probably without
realizing it. Consider the following code:

    
    
      $('.myelements').filter(filterFunction).css('color', '#ff0');
    

You can follow along what this code does by following the dots. First, all
elements matching the ".myelements" class are returned, these are then
filtered, and the result of that gets a css modification.

Every time you wrote a method that returns an object, you already did monads
in principle. They can be used to chain things together. While Haskell
provides semantics that explicitly deal with the mechanics of monads, pretty
much every dynamic language has them.

Here's what it looks like in PHP, for example:

    
    
      $obj
        ->bla()
        ->beep()
        ->borp();
    

You can use monads to make your code more concise, to simply chain function
calls, or to iterate over a list, but they're also often used when sifting
through databases since filtering and applying attributes to items. For
example, an SQL-like database query might be expressed like this:

    
    
      table1.select(all).where({ ref : 1 }).orderby('id');
    

Of course, that's an API which is a bit dishonest since it acts as if the code
really goes through all those items (which would be a performance disaster
with huge databases), so when you see something like this it's often just a
shortcut to assemble a query. And this goes for monads in general, they're
really more of an API style. A monad itself is an abstract concept, a
paradigm.

 _> Also, it's suspicious that pg has never once mentioned monads_

For a theory why pg specifically hasn't written anything about monads yet,
here's an article that I believe was on HN in the past about the difficulties
of comfortably using monads in Lisp:
[http://marijnhaverbeke.nl/monad.html](http://marijnhaverbeke.nl/monad.html)
\- but I guess the short explanation would probably be that Lispers tend to
deal with monad-worthy problems in other ways.

~~~
freyrs3
While there might be some structural similarity of function chaining to bind
operators in Haskell, jQuery isn't a monad. The burden of proof for being a
monad is explicitly demonstrating adherence to the monad laws. To quote
prolific Haskeller Don Stewart "I am pretty skeptical that the jQuery API as a
whole could satisfy the monad laws".

------
jkrems
I really wish people would start treating languages as languages. `(m >>= f)
>>= g ≡ m >>= (\x -> f x >>= g)` is _not_ easy to understand because the
symbols don't have names. I can't read it. How is "m >>= f" pronounced?

~~~
trobertson
> How is 'm >>= f' pronounced?

How do you pronounce '4 + 5'? It's easy, because you don't try to pronounce
'+', you pronounce 'plus'. With '>>=', there are a few names that all mean the
same thing.

There are two primary names, 'bind' and 'chain', and a description 'feed':

bind (the result of) m to (the input of) f

chain (the result of) m into f

feed m to f

There are probably a couple more things that can be said, in different
contexts, but these three cover the majority use case.

~~~
jkrems
Thanks for the very complete answer!

That those terms are not mentioned in the article (titled "What I Wish I Knew
When Learning Haskell", so apparently meant for people being new to the
concepts) is my actual criticism. The ability to instinctively replace those
symbols with the appropriate terms is something that has to be learned - and
easy to forget to teach. He's talking about "don't learn monads by analogies"
because he's maybe not fully aware that he is using "bind", "chain", "feed"
when actually reading those expressions - all of which are analogies.

------
parallelist
Anyone got any solutions for cabal hell? I barely use Haskell because 50%+ of
everything I try to install fails and I don’t have a freaking clue how to fix
it.

~~~
mhitza
If you're using cabal to install applications from hackage, it gets a bit
hairy. But if you just want to resolve your dependencies

    
    
        mkdir project
        cabal sandbox init
        cabal install dependency
        cabal repl # for a ghci instance inside the sandbox
    

Recommended is still to generate a .cabal file and specify your dependencies
there.

------
freyrs3
It's humorous that most of the article is _not_ about monads, yet the entire
discussion around it is about monads. Seriously, monads are only a small part
of Haskell.

------
szany

      SET : Group
      MAN : Lie Group
      TOP : Topological Group
      GRP : Objects
    

The last line should really be "GRP : Abelian Group"

See [http://en.wikipedia.org/wiki/Eckmann-
Hilton_argument](http://en.wikipedia.org/wiki/Eckmann-Hilton_argument)

------
codygman
As someone who has been learning haskell for the past 6~ months this is very
valuable. I will be studying this and toying around with the examples quite a
bit, thanks for this resource!

------
platz
Not sure how I feel about this thread.

A hundred folks who probably haven't investigated how to effectively teach
adults, try to explain monads in the space of 3 tweets (probably not the best
medium).

And then we wonder why folks steer towards less abstract tools. For every
person who visits which gains monad satori, how many have we turned away,
never to return?

------
ufo
Holy shit how did I never know about the ":r" shortcut??? This changes
everything.

