Sunday, January 09, 2011

Monads in C#–1. Introduction

Here’s the complete series:
1. Introduction.
2. What’s the point?
3. Creating our first Monad.
4. Linq loves Monads.
5. Maybe ;)
6. Introducing a simple parser.
7. Turning our parser into a Monad.
8. Monads in C#-8. Video of my DDD9 Monad talk

I’ve foolishly volunteered to give a talk at DDD9 on the 29th January: ‘Monads! What are they, and why should I care?’:

“Or: How to bend Linq syntax to your will.

These days, monads are the "celebrities of programming language theory". But they also inspire fear in the hearts of lowly imperative programmers like myself. However they are a very useful and powerful abstraction and they pop up everywhere. C#'s Linq syntax is Monadic, for example. Having an understanding of Monads will give you the conceptual tools to greatly simplify many programming challenges, from dealing with nulls and managing state, to asynchronous programming and parsing. This talk will be mostly C#, but I will also be introducing a little F# and even some Haskell.”

Why foolish? Well, firstly, because I’ve only recently come to a limited understanding of what Monads are myself, and I’m fully aware that I’ve got a great deal more to learn. There’s a very good chance that I might not understand some important points, or that I’m just wrong about some things. Secondly, because it’s taken me several books, a gazillion blog posts and articles and upwards of a year to get to my limited understanding. To expect to transfer this to someone else’s brain in an hour’s presentation is completely unrealistic.

So why do it? Simply because Monads are such an extraordinarily powerful concept. I’ve had my mind blown. I’ve been on a wonderful intellectual voyage of discovery this past year, and I want to get other people excited about Monads and functional programming in general. Few people seem to be talking about Monads in the C# community which is a shame. Maybe I can correct that in some small way.  I hope that the people who come along to the talk will get something useful out of it. I don’t expect for someone who’s never come across the concept before to walk out with a real understanding, but maybe they will walk out motivated to learn more with some pointers about where to look. Maybe someone who’s spent some time looking at Monads, but not quite grasped them yet, will find some more pieces of the puzzle? And lastly, and rather selfishly, I’m hoping that by publically explaining my understanding, I will get some feedback and garner insights from people who understand the concept far better than I.

To get my own thoughts in order and provide a starting point for anyone else who’s interested, I’m going to write a series of posts about Monads in C#. This is another foolhardy enterprise as Brent Yorgey so eloquently points out. But so long as you take it in the spirit that everything in this blog should be taken; that it’s just me learning in public, we should get along fine :)


Jordan said...

Curious - what books did you read in connection to your research into Monads?

Mike Hadlow said...

Hi Jordan,

I'm going to wind up the series with a big list of books, blog posts and articles. Watch this space :)

Joel said...

Hi Mike,

Still interested in the list of books, blog posts, and articles. Thanks!