News for hackers. Stuff that matters.
Re:Functors, applicatives, and monads in pictures by seldo on Sunday May 19, 2013 @12:43AM (#5731826)
They chose a deliberately over-simplified example in order to demonstrate the core concepts; I don't begrudge them that (nobody needs a program that prints out "Hello World!").

The audience level of this piece is confusing -- it looks like a friendly, cartoony introduction for a general audience, but it actually assumes you already know Haskell.

Basically, you and I are not the intended audience.

Re:Functors, applicatives, and monads in pictures by balloot on Sunday May 19, 2013 @12:59AM (#5731871)
I know a bit about Haskell and have played with it. I guess it just seems like functional languages are for those who want to solve super complicated mental exercises when doing tasks that would otherwise be easy using a traditional language.

There are very few things in the world of CS/programming where I can't follow along and grok what's going on, especially when laid out in a tutorial form like this. The fact that I was totally lost when reading this is not a good sign when you're trying to get people to adopt your school of thought.

Re:Functors, applicatives, and monads in pictures by ww520 on Sunday May 19, 2013 @03:12AM (#5732131)
To the other people saying that pure functional programming is the reason that it's difficult to explain Haskell to other programmers and blaming them for doing too much imperative style coding, it's the wrong assumption. Pure functional programming has nothing to do with the difficulty in explaining Haskell. Pure functional programming is in Lisp/Schema/C#/Clojure/and other languages and people have no trouble learning it.

Haskell is difficult to explain because it has many terms, vocabulary, and syntax that are not familiar to people. It's ML-style syntax doesn't help.

Most tutorials targeting Haskell beginners implicitly assume the readers already know the Haskell syntax, terms, definition, etc, and go right ahead to explain the advance concepts like functor, applicative, or monad. It's like teaching them how to fly before they know how to walk, in the Haskell land. No wonder Haskell beginners are frustrated with any of the monad tutorial.

The thing is. Stop. Stop telling beginners what monad is. Just show them the basics in Haskell and get something useful going. Get them to write and use Haskell programs. When the need arise, they will look into what monad is. Programmers have used STL plenty without knowing the internals of C++ templates. Same thing can be done with monad.

Re:Functors, applicatives, and monads in pictures by egonschiele on Monday May 20, 2013 @01:00AM (#5735992)
Just to follow up on your other comment:

> When the need arise, they will look into what monad is.

I couldn't have said it any better. This is the exact scenario that this post is aimed at.

Re:Functors, applicatives, and monads in pictures by chongli on Sunday May 19, 2013 @02:20AM (#5732035)
>There are very few things in the world of CS/programming where I can't follow along and grok what's going on, especially when laid out in a tutorial form like this.

That's probably because most of the tutorials you've followed have adopted an imperative style of programming. It is easy for you to follow along because it really isn't all that different from what you are accustomed to. Functional programming really is a different beast entirely. It's often said (though I remain skeptical) that it's actually easier to learn functional programming as a non-programmer than to un-learn your imperative programming tendencies.

>The fact that I was totally lost when reading this is not a good sign when you're trying to get people to adopt your school of thought.

I don't think that's a fair assessment. You could say the same thing about starting from functional programming and switching to imperative; some people actually do this! They find the idea of mutating a variable to be completely counter-intuitive!

Re:Functors, applicatives, and monads in pictures by balloot on Sunday May 19, 2013 @04:01AM (#5732217)
I would love to meet this mythical being who understands functional programming but has no clue how to write an imperative program.
Re:Functors, applicatives, and monads in pictures by wwweston on Sunday May 19, 2013 @04:53AM (#5732302)
While going through a Math undergrad program in the mid-90s, I noticed that some number of my fellow students had a distaste for programming.

As someone who'd done reasonably well with both and had thought of them as heavily overlapping areas, I thought this was interesting, so I tried to poke at this a bit.

One of the concepts I got back from a talented classmate was basically a complaint about mutable variables -- that in an algebraic description of relationships for a given system what we call "variables" are less variable and more "unknowns" which represent fixed if unidentified quantities. Others similarly noted there was something absurd about writing out equations like "x = x + 2".

Re:Functors, applicatives, and monads in pictures by tripzilch on Sunday May 19, 2013 @05:27PM (#5734455)
I can see why they think that, but the important thing is that "x = x + 2" is not an equation. It's just an arguably poorly chosen operator (Pascal gets this right and uses ":=" for assignment).

The trick is to stop reading that line (in your head) as "x is equal to x plus two", but instead "x is assigned the value of x plus two".

Re:Functors, applicatives, and monads in pictures by wwweston on Sunday May 19, 2013 @07:56PM (#5734988)
> It's just an arguably poorly chosen operator (Pascal gets this right and uses ":=" for assignment).

That's one potential takeaway, but when I turned over the complaint in my mind, this was arguably another way of complaining about mutability.

Also, given the timing, it's pretty likely that one of the languages they'd learned was Pascal: that was the language of the first few CS classes at my school in the 90s, it was also the language of any high school curriculum that led to the AP exam, and Borland's Pascal products were still pretty popular PC dev environments.

They could've also been unlucky and received their first exposure by choosing to do a numerical analysis class in Fortran or C, of course. :/

Re:Functors, applicatives, and monads in pictures by Shorel on Sunday May 19, 2013 @10:34AM (#5732937)
Most students of pure mathematics get functional programming much faster than imperative programming if they have never programed before.

Source: all my classmates.

Re:Functors, applicatives, and monads in pictures by ParahSailin on Sunday May 19, 2013 @01:01PM (#5733484)
People who exclusively use Excel spreadsheets to program is a good example. Most people who use spreadsheets never use functions in a cell whose effect is to change the function or value in another cell.
Re:Functors, applicatives, and monads in pictures by cynwoody on Sunday May 19, 2013 @03:29AM (#5732163)
> They find the idea of mutating a variable to be completely counter-intuitive!

The notion of mutation is implicit in the definition of "variable."

Re:Functors, applicatives, and monads in pictures by chousuke on Sunday May 19, 2013 @04:39AM (#5732277)
Except it isn't. Unless you learn imperative programming before you learn basic algebra, the first definition of a variable you encounter is that of an immutable one.
Re:Functors, applicatives, and monads in pictures by Shorel on Sunday May 19, 2013 @10:33AM (#5732934)
Actually an algebraic variable is named that way because you can change it every time you do the exercise.

So you don't change it during one exercise, but you can and certainly will change it when you change the exercise to another one with the same principle.

Re:Functors, applicatives, and monads in pictures by tikhonj on Sunday May 19, 2013 @11:32AM (#5733130)
That behaves just like a function: the values of the variables change each time you call the function but not within. When you do a new exercise, it's really a bunch of new variables with the same name--variables cannot affect each other between exercises!

That's not analogous at all to mutation.

Re:Functors, applicatives, and monads in pictures by chongli on Sunday May 19, 2013 @11:17AM (#5733062)
Right, but you're not mutating it in the middle of your calculation.
Re:Functors, applicatives, and monads in pictures by Gravityloss on Sunday May 19, 2013 @01:47PM (#5733687)
If you have a sum for example sum_{x=1}^{10}x^2, x definitely changes quite many times during your calculation.
Re:Functors, applicatives, and monads in pictures by chongli on Sunday May 19, 2013 @03:43PM (#5734136)
No, that's an example of recursion. With recursion you aren't mutating the variable, you're shadowing its definition. This is a subtle, but important, distinction.
Re:Functors, applicatives, and monads in pictures by Gravityloss on Sunday May 19, 2013 @04:51PM (#5734348)
Recursion? The "function" is not calling itself at all. I'm quite sure that is not recursion, neither in the mathematical nor computer science sense.
Re:Functors, applicatives, and monads in pictures by chongli on Sunday May 19, 2013 @05:57PM (#5734570)
Recursion is how you implement a sum without mutation (an imperative loop is how you'd implement it with mutation). You've merely given me the function written in a declarative notation.
Re:Functors, applicatives, and monads in pictures by Gravityloss on Tuesday May 21, 2013 @11:54AM (#5744509)
I think we are talking about different things.

For example, if you integrate x^2 dx from x=0 to 10, x changes continuously.

It's clear that x is not fixed to any particular value.

Re:Functors, applicatives, and monads in pictures by chongli on Wednesday May 29, 2013 @05:32AM (#5785400)
Yeah, you're talking about the mathematical definition of a bound variable. Functional programming languages use this whenever you define a function:

   foo x = x * 2
Every time you call foo, x is bound to a new value (but only within the scope of foo). This is not the same as mutating some memory location associated with the name x.
Re:Functors, applicatives, and monads in pictures by chousuke on Monday May 20, 2013 @01:26PM (#5738792)
x does not mutate at all. x is simply a placeholder for the values in the series of additions that your expression expands to, that is, 1² + 2² + 3² + ... + 10². Or stepwise, to 1² + sum_{x=2}^{10}x² etc. all three expressions are equivalent, and no mutation of x occurs anywhere

The series of additions can also be infinite, in which case it would be quite difficult to expand fully, but that doesn't prevent you from doing mathematics with it.

Re:Functors, applicatives, and monads in pictures by ams6110 on Sunday May 19, 2013 @11:21AM (#5733086)
An algebraic "variable" is perhaps better called an "unknown"
Re:Functors, applicatives, and monads in pictures by tel on Sunday May 19, 2013 @02:19AM (#5732030)
This is a pretty common idea, but it's because pure functional programming is, at first, all about getting you to significantly change your programming POV. It's a shift for anyone---in fact, the better you are at understanding C/Ruby/Python/Perl/Java/ObjC/Lua the harder a shift it is.

At the end of the day, you adopt a new way of thinking that is incredibly clarifying for all programming. One simple thing is that it allows you to "see" where state, re-entry, evaluation, IO, and failure are happening in a program---things that you are blind to when you're used to languages where the answer is "always".

This article isn't even meant to explain why you ought to learn Haskell. It's more like a signpost on your path to mastery, should you begin walking it.

Re:Functors, applicatives, and monads in pictures by egonschiele on Sunday May 19, 2013 @01:14AM (#5731912)
Post author here. I'd love to know where you stumbled!
Re:Functors, applicatives, and monads in pictures by olavk on Sunday May 19, 2013 @12:09PM (#5733263)
In other communities, new tools and abstractions are generally "sold" by explaining how they are useful for solving specific problems. Not so in the Haskell community though.

The Haskell community has a longstanding tradition of using condescending language ("fmap is from the street, fmap is hip to contexts."), and deliberate childrens-book style metaphors and illustrations in tutorials.

The theory seem to be that if monads haven't been adopted by mainstream developers yet, it is just because the presentations haven't been dumbed-down and kiddie-friendly enough yet. (If only they from the onset had called "monads" for "warm fuzzy things" instead, surely developers would not be so scared of them!)

The strategy have not yet borne fruit, though.

Re:Functors, applicatives, and monads in pictures by egonschiele on Sunday May 19, 2013 @12:54AM (#5731854)
Right. I'm hoping to make monads easier to understand, but you need some knowledge of functional programming to build on. If I had tried catering to the complete beginner, this post would be much longer and not as focused :)
Re:Functors, applicatives, and monads in pictures by seldo on Sunday May 19, 2013 @01:00AM (#5731876)
That's fair enough! Perhaps a sentence at the beginning to establish your goal would prevent comments from confused clueless people like myself.
Re:Functors, applicatives, and monads in pictures by egonschiele on Sunday May 19, 2013 @01:10AM (#5731902)
Done!
Re:Functors, applicatives, and monads in pictures by chii on Sunday May 19, 2013 @12:56AM (#5731860)
> They chose a deliberately over-simplified example in order to demonstrate the core concepts;

i wish people who are writing guides on these topics pick a non-simplified situation where using this method makes for an easier program (vs using the "traditional method"). The comparison and contrasting of a functional/monadic approach vs a procedural approach in a complex scenario might actually shed more light - at least, for some one who is familiar with the procedural approach.

Re:Functors, applicatives, and monads in pictures by balloot on Sunday May 19, 2013 @01:03AM (#5731888)
I would love to see this! One of the fundamental issues I have with functional languages is every example I see is basically "See this thing that's relatively simple and uncomplicated in Ruby/PHP/C/whatever? Here's how to do that in Haskell!" And the Haskell version ends up being extremely unintuitive and complicated. Bottom line is I still don't know for which situations functional languages are a "good" tool.
Re:Functors, applicatives, and monads in pictures by gizmo686 on Sunday May 19, 2013 @01:29AM (#5731951)
I think the problem is that the Haskell version is actually very simple, to the person writing it. Its a classical problem of teaching, it is extremly easy to forget how something looks to new people.
Re:Functors, applicatives, and monads in pictures by egonschiele on Monday May 20, 2013 @01:03AM (#5736004)
Again, if you tell me where you're stumbling, I can fix it. I see your complaints all over this post and you haven't responded to me once.
Re:Functors, applicatives, and monads in pictures by nandemo on Sunday May 19, 2013 @10:03AM (#5732863)
That approach is used at length in Real World Haskell. For example, some code for key-value lookup is shown that deals with a series of Maybe foo values, first with a cascade of guards (think a cascade of if-else's), and later with monad syntax. There are other examples.

http://book.realworldhaskell.org/read/programming-with-monad...

Re:Functors, applicatives, and monads in pictures by lobster_johnson on Sunday May 19, 2013 @09:05PM (#5735231)
I enjoyed RWH much more after having learned a lot of the basics. It starts off simple, to be sure, but then quickly jumps into the deep end. For a book that's about real-world stuff it gets into a lot of unnecessary detail, too, that should have been deferred until later "advanced" chapters. I appreciated the hands-on approach to real examples, though.

"Learn You A Haskell For Great Good" [1], while much less complete, is the best intro I have found so far. It's also free.

[1] http://learnyouahaskell.com/