
What I Wish I Knew When Learning Haskell - gtani
http://dev.stephendiehl.com/hask/#%281%29
======
cies
I think these 3 slides really stand out, they show with how little code one
can write serious software in Haskell...

1\. ACID (acid-compliant persistance on Haskell data types, with indexes)

<http://dev.stephendiehl.com/hask/#(27)>

2\. Pipes (program on streams of data)

<http://dev.stephendiehl.com/hask/#(30)>

3\. Scotty (micro web-framework, a la Sinatra or Flask, on top of the super
fast Warp server) <http://dev.stephendiehl.com/hask/#(32)>

~~~
cscheid
About your point 3), it's worth noting the "toHtml" call buried in that
example.

Haskell is both strongly and statically typed. That type boundary between
strings and HTML output is enforced by the compiler, and it automatically (and
provably!) prevents a metric crapload of injection attacks.

~~~
tel
To bring this into sharper detail, notice that there's the "OverloadedStrings"
compiler directive at the top of the file. That means that "hello " and "!"
are of type Html, different from String. Strings can only be concatenated with
Html after they've been escaped.

For more elaboration, see [http://blog.moertel.com/posts/2006-10-18-a-type-
based-soluti...](http://blog.moertel.com/posts/2006-10-18-a-type-based-
solution-to-the-strings-problem.html)

------
rtpg
I disagree completely with his analysis on monads.

If you just present the abstract structure, you cannot understand _why_ you
even want (for example) the do notation as a language-level concept. I think
this is more of a case of bad examples than anything.

Trying to represnent monads via analogy works badly too though, because monads
as used in haskell are more about programming pragmatics than modelling real
world objects.

The way to explain it should be :

\- Show real code

\- Explain why we use this structure to write our code .I think a good
explanation is to ...

in Jquery you do things like chaining to build things.

$('selector').css('color','red').filter(someFilter).css('color','blue')

Chaining APIs are useful because you're combining computation('chaining'), and
you don't want to have to carry around a value everywhere. You want to do the
same thing in Haskell obviously.

Haskell doesn't have dot notation, so you can't do this style exactly. First
idea would be to do something like

let x=jQuery document selector in let y=css x 'color' 'red' in let z=filter y
someFilter in ...

This is a pain, especially considering that the 'chaining' here is
mechanically obvious

let [n+1] = f [n] [whatever] in let [n+2] = g [n+1] ....

So monads, in the most pragmatic sense, allow you to represnt this form of
computation. You end up with

jQuery 'selector' >>= css 'color' 'red' >>= filter someSelector >>= css
'color' blue'

You implicitly carry around your computation.

)

In any case the Maybe monad is the worst example ever, just like how a
singleton group is a bad example. Sure it's the 'simplest' , but it's far from
the most typical

~~~
freyrs3
The problem with bringing jQuery into the discussion is that it's _not a
monad_. Don Stewart explains this more elegantly than I can [1] but the crux
of the argument is that chaining together computations is not enough to be a
monad, one needs to prove that the monad laws hold.

[1] [http://stackoverflow.com/questions/10496932/is-jquery-a-
mona...](http://stackoverflow.com/questions/10496932/is-jquery-a-
monad/10499668#10499668)

~~~
sordina
The majority of the 'proofs' I've seen that jQuery isn't a monad seem to
either be probabalistic, or use the "Burden of Proof" argument to show that
since nobody has proved that jQuery is a monad, we should assume that it
isn't. I agree that jQuery probably isn't a monad, but it rubs me the wrong
way that people aren't willing to say "It has yet to be determined if jQuery
is a monad".

Obviously it is trivial to show that it isn't a monad by the fact that
javascript permits any action in pure code, but by the same token,
unsafePerformIO proves that Haskell has no monads. I think it's fair to
operate under a reasonable subset of jQuery functionality when asking the
question.

~~~
xyzzyz
The point is that the concept of monad is a very concrete thing. Monad is not
"chaining operations". Monad is not "returning actions instead of values".
Monad is not some vague concept.

If JQuery is a monad, what are the monadic operations that make it a monad?
What are its equivalents of return and bind (or any other equivalent
definitions of a monad)? It's not a tricky question.

Saying that JQuery is a monad without saying what its return and bind are is
equivalent to saying that whistling is Turing complete without saying how you
can compute stuff with whistling. The problem here is not whether whistling is
Turing complete or not, the problem is _what it would even mean for whistling
to be Turing complete_.

This means that as of now, as long as you don't tell us what are the monadic
operations of JQuery, _it cannot be determined_ whether JQuery is a monad,
because this question does not make sense yet.

~~~
jes5199
JQuery is an implementation of the List monad.

~~~
tome
JQuery is an enormous API, so it is implausible that it is an implementation
of the list monad. Some aspect of it might be the list monad. If so, which?

~~~
jes5199
Most jQuery methods are functions that are mapped over a collection of
elements. Many verbs return collections themselves, which are concatted
together into the resulting collection. That's analogous to how List works.

So $(element) implements return as a -> [a]

And bind is mapcat, which is called by most of the jQuery methods.

Yes, that's a subset of the API, but it's the subset that distinguishes jQuery
from any other collection of helper functions.

~~~
nbouscal
$(element) does not implement return. $($(element)) gives $(element), which
means that it does not work on an arbitrary datatype, which means that it does
not implement return.

jQuery may be analogous to a monad. That doesn't make it a monad.

~~~
jes5199
There's a lot of stuff in jQuery that's not monadic, but that doesn't
disqualify it. There's no law that return(return(x)) has to be defined - a
Haskell List of Integers is still monadic, even though return(return(1)) would
be a type violation. And undefined code can do whatever you want and not be a
problem for the definition.

~~~
nbouscal
No, I'm sorry, you're wrong. return(return(1))::[[Int]] gives you [[1]]. For
something to be a monad, it must be a polymorphic type of kind * -> * . That
means it must be a type constructor which takes any type as a parameter. Any
type, including a concrete type constructed from the type constructor you are
declaring an instance of Monad.

~~~
jes5199
No, a monad is not something that implements the Haskell typeclass Monad. A
monad is anything that satisfies the monadic laws - and they don't say
anything about polymorphism, only associativity and left/right identity. I
assert that you can implement a structure that satisfies those laws without
having return(return(x)).

Can you disprove that?

(P.S. you are wronger)

~~~
jules
Yes, I can disprove. This has nothing to do with Haskell, I'm only going to
use the monad laws. The first monad law says:

    
    
        bind f (return a) = f a
    

For all a and f. Therefore, by substituting f = id, and a = return x:

    
    
        bind id (return (return x)) = return x
    

If, as in jQuery, `return (return x) = return x`, then we can simplify to:

    
    
        bind id (return x) = return x
    

Now, applying the first monad law again, we get:

    
    
        x = return x
    

So return must be the identity function. But jQuery's $ is not the identity
function. Contradiction. QED.

~~~
jes5199
I like your answer, but I'm going to refuse to concede that it's correct, out
of stubbornness.

No wait, I can do better. For _some_ values, $($(x)) is the equivalent to
$(x). So, for some values $(y) = y. So for the domain of y where $(y) = y, $()
is the identity function, and now it's a monad.

~~~
nbouscal
Now it's a monad how? What category is it a monad on, and in what way does it
satisfy the monad laws? Are you really saying that sometimes $() is return and
other times it's id, and presto magico it's a monad?

~~~
jes5199
Look, if you want to have a discussion with a reasonable person who's going to
try to follow your arguments and retort them under the exact same level of
rigid discourse that you are using, maybe you should try someone other than
me.

But yes, presto magico. I am outwitting your narrow-minded mathematics.

~~~
nbouscal
Well, as long as it's all in good fun :-)

------
mercuryrising
He's got some good stuff on his website (didn't get much from the slides as
I've only dabbled in Haskell a couple times).

<http://www.stephendiehl.com/posts/essential_haskell.html>

<http://www.stephendiehl.com/posts/types.html>

~~~
egeozcan
I would suggest these for the starters: <http://shuklan.com/haskell/>

~~~
mercuryrising
Wow those are some awesome lectures. Thanks!!

------
Kiro
I'm a PHP developer and I didn't understand one single thing in this
presentation. Any reason I should learn Haskell?

~~~
MichaelGG
If you're really happy and think things are dandy and don't really have an
alternative, then no, stay away from Haskell and FP. As others have commented,
you will not be able to code the same way after you have learned functional
programming.

While FP doesn't solve everything, it does massively reduce the amount of code
you write. Where I work, I started writing everything I owned in F#. After a
while, I noticed all other new projects were being written in F#, even by
people that had previously told me "we are sticking with C#". Why? Because
they feel bad having to write loads more code to get the same work done.

Every time I have to work in C#, I'm constantly annoyed about how verbose even
the tiniest things are, how much work there is.

Beware, PHP's syntax for lambdas is incredibly verbose (and IIRC there are
some odd limits on how you can use them), so if you become "enlightened" then
continue to try to apply it in PHP, you are in for a rough ride. Even C# which
slightly embraces functional concepts, gets pretty nasty if you write in any
sort of real FP style.

~~~
boothead
Where do you work that you can just take a decision like "I'm going to use
language X" if you don't mind me asking?

How did you deal with concerns (which I assume your manager must have had)
about skilling up the rest of your team, and your increasing component of the
bus factor? [1]

I tried to make a case for F# for some parts of our codebase and got shot
down!

[1] <http://en.wikipedia.org/wiki/Bus_factor>

------
yummyfajitas
There is a great point buried in slide 8. The best way to learn to use monads
is... _Use monads in real code._

------
thomasjames
Does anyone know the best book for learning Haskell? I have heard it is good
for just thinking about programming in new ways and keeping your mind agile. I
can really only learn a language with a good structured book with exercises at
the end of each chapter. Haven't found something definitive yet, but would be
open to suggestions.

~~~
ericssmith
The 'best' introductory book for learning Haskell is Graham Hutton's
'Programming in Haskell'. It focuses on the essentials of the language, and
its clear, spare presentation is in the spirit of Haskell itself. And it is
accessible to everyone.

~~~
thomasjames
I was actually thinking of going with this since it seemed to focus on the
foundations of lambda calculus and is also fairly condensed. I was thinking I
might try "The Haskell School of Expression: Learning Functional Programming
through Multimedia" as a follow up just cause it sounded satisfying.

~~~
ericssmith
Hudak's 'Haskell School of Expression' is also full of much Haskell wisdom and
spirit.

------
lobster_johnson
Slides don't work in Sarari on iOS. Is there a static version?

~~~
freyrs3
<http://dev.stephendiehl.com/hask/flat.html>

~~~
lobster_johnson
Finally a proper answer. Thanks!

------
pervycreeper
Is there a recording of a talk to match these slides?

------
simpsond
This post somehow triggered me to grab "Learn You a Haskell for Great Good"
this morning and I have been gorging on it since then. I've had quite a few
"holy shit" moments.

------
mercurial
Pretty comprehensive (including plenty of things I haven't had time to look at
into more details yet). Bonus points for using applicative style, which makes
it a lot easier (and clearer) to apply function to value inside monads.

------
Uchikoma
"Haskell Propaganda: OOP programming couples code and data together in
unnatural ways. Typeclasses are a better way of structuring logic."

should be

"Inheritance couples code and data together in unnatural ways. Typeclasses are
a better way of structuring logic."

~~~
papsosouid
How so? An OOP language without inheritance would still be coupling code and
data together "in unnatural ways". It is the object that is doing the
coupling, not inheritance.

------
kzrdude
Some of the slides need explanation.

What is the value of -ddump-simpl as shown here?
<http://dev.stephendiehl.com/hask/#%2816%29>

~~~
ufo
As freyrs points out, GHC "desugars" all Haskell code into a very small
intermediate language that is similar to the System F lambda calculus. In the
slide you linked you can kind of see that: you start with two doubles for 2
and 3 (created wit the `D#` function) and then you add them with the addition
operator. Notice that this exposrs the implementation of typeclasses: the
overloaded + operator is actually a function of three arguments and the first
argument is the "vtable" that has the actual number operations.

I don't know why he is doing all of this though. The slides themselves don't
give much context...

------
rtkwe
Nothing like getting a pile of slides without any presenter notes or anything
else helping to interpret the slides.

------
sordina
This is very good. I'm getting a bunch of new tricks out of this even though I
regularly use these libraries.

------
theicfire
Recording would be great! I feel like I only got half of the (good)
information without it.

------
mwerty
I first learnt Haskell ten years ago for a class. Mostly toy problems. Looking
to get back into it. What books are good to learn to build actual apps -
especially user input, UI, etc.?

~~~
cies
realworldhaskell is good for general purpose.

i think the yesod o'reilly book (free on yesodweb.com) is a very good one if
you dont mind to make web apps.

------
yayitswei
I saw a blank page and immediately thought "how poetic".

