21
votes

Let me state up front that I have an infantile understanding of Monads. I have read the various threads on Monads here and have done a few hours of study on the concept. I hardly feel comfortable with the term, but I think it is safe to say that I generally understand what a Monad is/does.

I'm a C# developer who is looking to improve the way I work. What would help me further in my Monaducation is see a real world application of a Monad in C# (i.e. via a linq SelectMany() or somesuch) that is clearly an improvement over other ways of solving the same sort of problem in oldskool C#.

Has anyone seen such a beast?

8
Besides this being an interesting question, I liked the term "Monaducation".Rafa Castaneda
I'm starting to read John Skeet/Tomas Petrecek's Real World Functional Programming amazon.com/Real-World-Functional-Programming-Examples/dp/… that covers come of this ground.Kevin Won
the reactive framework (msdn.microsoft.com/en-us/devlabs/ee794896.aspx) is a good example, it seems to me.Kevin Won
More Monaducation: research.microsoft.com/en-us/um/people/simonpj/papers/… bet Monad explantion I've read (and bonus points since it was also among the first ever written!)Kevin Won
It is possible to do the reverse, and for instance, thinking about Linq to Events as an inspiration for doing monadic reactive programming in Haskell.Alexandre C.

8 Answers

11
votes

Here is one such scenario: you want to author a parsing library (a nice example of an embedded DSL), and you discover that the best ones are monadic parser combinator libraries. So you write it leveraging LINQ syntax sugars to author C# code that has the same structure as the grammar of the language you're parsing, and you get the benefits of an awesome programming model for on-the-fly semantic analysis and error-recovery. See this blog for a description.

5
votes

Find Pythagorean triples:

  var r = from a in Enumerable.Range(1, 25)
          from b in Enumerable.Range(a, 25-a)
          from c in Enumerable.Range(b, 25-b)
          where a*a + b*b == c*c
          select new [] { a, b, c };
4
votes

Here is one such scenario: you want to write code that makes sequential async calls (e.g. IO) without holding threads, but you don't want to write the hopeless tangle of spaghetti that the async programming model (BeginFoo/EndFoo) forces you into. So you can use a monad and LINQ sugars and write code that looks straight-line but it releases/switches threads throughout. See this blog for a short description.

3
votes

One example is simplifying null checks using the Maybe monad as shown in this article.

0
votes

LINQ is used in many solutions (and often requested in questions) here on StackOverflow. Review questions with the LINQ tag and you will see real world usage.

0
votes

Programming with monads is declarative, describing what you want at a high level rather than the low-level details of how to generate it.

See the exercises at the end of Brian Beckman's state-monad talk on Channel 9.

0
votes

I recently blogged about refactoring a typical imperative real-world C# code (a function in NuGet) to functional, monadic style (more concretely, using the Maybe monad). I did my best to do it in little steps, explaining the rational behind step, so I think it helps in understanding how monads are useful.