dave^2 = -1

A software development blog by some bloke called Dave

From two functions to tuples with a mad Haskeller

I love finding neat little bits of Haskell that do things in ways I haven’t really thought of before. This usually happens when I come across a simple yet slightly clumsy way of doing something, and embark on some mad experiments to find alternative approaches (usually ending in a trip to #haskell.au on Freenode). These alternatives may not result in anything usable, but they often prove to be fun learning experiences for me.

A recent example of this was the following adventure in passing the same input to two functions, and getting the output as a tuple.

"Stand back… I'm going to try Haskelling!" Original image source: Muppet Wiki
"Stand back… I’m going to try Haskelling!"
Original image source: Muppet Wiki

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.

Threading and Lazy<T>

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.

State monad

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 :)).

Reader monad

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.