# YAMT (sorry)

This post is written to try and solidify my own understanding of what monads are, how they work, and why they are useful. I’m very much still learning this stuff myself, so please comment if you have any corrections or if any part of this doesn’t make sense.

My previous attempt to describe monads used Haskell examples, so I thought I’d try a more language-agnostic explanation. In this case most of the code samples are written in the closest we have to a universal language: JavaScript.

# Lazy<T> monad instance in C#

One of the most surprising and useful things I’ve learned about .NET this year is that it has quite good support for monads as of .NET 3.5 (it’s just missing higher-order polymorphism). What’s more, for those allergic to monads, you don’t need to understand anything in that previous sentence to follow this post. :)

In this post we’ll implement the couple of functions necessary to be able to compose instances of Lazy<T> together in interesting ways using LINQ and LINQ comprehensions.

Xerx and I have been playing around with using Lazy<T> for one-time, asynchronous initialisation of values, and we stumbled across the various threading options that Lazy<T> supports. After puzzling over the documentation for a while I thought it was probably easier just to run a few examples.

# Some optional, functional goodness in C#

For this post I wanted to show you a small yet extremely useful bit of functional programming that you can apply right away to your current C# (or VB.NET) project. While there’s solid theory underpinning it, we won’t need any of that to be able to apply it, so we’ll jump straight to the practice. (I love the theory, so this is quite a sacrifice I’m making for you! ;)) As an added bonus, we’ll also get a good stepping stone for starting to apply more of these practical FP ideas in our everyday code.

# Catamorphisms

According to Wikipedia a catamorphism "denotes the unique homomorphism from an initial algebra into some other algebra".

Uh huh. Sure.

I’ve got an admittedly very basic understanding of this concept, but I thought I’d share the small amount I have been able to learn and apply, as I’ve found it to be a useful tool for working with types.

Liquid error: uninitialized constant Liquid::StandardFilters::Iconv

During some recent work I found the need to use the State monad. Unfortunately I had no idea how to do this.

In this post we’ll retrace the steps I took while trying to generate random strings with Haskell. We’ll start by coding specific, non-monadic pieces to solve the problem, then generalise this by implementing our own State monad, before finally switching over to use Haskell’s implementation. By the end of it we’ll hopefully all understand the State monad (which is more than I can say for when I first started tackling this problem :)).

# It’s an imperative, mutable world out there

Liquid error: uninitialized constant Liquid::StandardFilters::Iconv

The Reader monad is used to pass one value as an argument to a number of function calls. This can be useful when you require some configuration or environment information accessible from a block of functions.

This monad is provided in Haskell’s standard libraries, but let’s have a go at creating it ourselves.

# Composition via scary-sounding maths terms

Last post we looked at composing lists of functions using folds. This let use write functions of type [a -> a] -> a -> a to compose lists of functions (take a list of functions a -> a, and return a single function a -> a).

Another way to do this relies on treating functions of type a -> a, also known as endomorphisms, as a monoid.

## Prologue (or "Why bother?")

Me-from-a-year-ago would have tuned out when someone dropped a monoid-bomb or similar term, assuming it was too complicated. Since then I’ve found lots of maths / category theory terms co-opted by computer science that represent surprisingly straight-forward and useful concepts. No Babel fish required, just a little bit of patience. :)

Even more surprisingly, I’ve found looking at this stuff both interesting and fun!