There is a lot of talk about monads these days. I have read a few articles / blog posts, but I can\'t go far enough with their examples to fully grasp the concept. The reaso
See my answer to "What is a monad?"
It begins with a motivating example, works through the example, derives an example of a monad, and formally defines "monad".
It assumes no knowledge of functional programming and it uses pseudocode with function(argument) := expression syntax with the simplest possible expressions.
This C# program is an implementation of the pseudocode monad. (For reference: M is the type constructor, feed is the "bind" operation, and wrap is the "return" operation.)
using System.IO;
using System;
class Program
{
public class M
{
public A val;
public string messages;
}
public static M feed(Func> f, M x)
{
M m = f(x.val);
m.messages = x.messages + m.messages;
return m;
}
public static M wrap(A x)
{
M m = new M();
m.val = x;
m.messages = "";
return m;
}
public class T {};
public class U {};
public class V {};
public static M g(V x)
{
M m = new M();
m.messages = "called g.\n";
return m;
}
public static M f(U x)
{
M m = new M();
m.messages = "called f.\n";
return m;
}
static void Main()
{
V x = new V();
M m = feed(f, feed(g, wrap(x)));
Console.Write(m.messages);
}
}