xsharpx
xsharpx copied to clipboard
Implement Terminal I/O
On 03/12/2013, Joe Albahari gave an excellent talk on functional programming using C# in Brisbane. Joe's introduction emphasised pure functions, referential transparency and the general thesis of functional programming.
Joe went on to introduce LINQ and the associated concepts. During this time, it was supposed that it is not possible to implement terminal I/O as a pure function. Console.ReadLine()
was used as the example (as was random generation) as examples of "not possibly pure functions."
It is certainly possible to achieve this. Indeed, it possible even in C#. The critical tool in the tool belt is known as the free monad. Although C# will not allow us to abstract on type constructors, we needn't do this to implement our free monad that is specialised. The rng project eliminates the supposition that random generation cannot be implemented as a pure function (it can and it has been), however it is written using the Scala programming language.
This issue addresses terminal I/O and a pure-functional implementation in the C# programming language. To achieve this, we will use the free monad. This technique has recently been popularised by Runar Bjarnason, but I have not ever seen it implemented for C#.
Let us begin.