One of the most interesting aspects of the Haskell language is the fact that features lazy evaluation. My interest in lazy evaluation led me to a post on Wes Dyers blog about lazy lists in C#. In his blog post he talks describes how to create a lazy list class in C# and demonstrates some classic lazy and functional methods of manipulating them. After reading this I decided to use his code as a basis to some further experimentation.
I started playing with his code and modifying it to make it easier to use. The end result was a class and a set of methods which allow you to easily use and manipulate lazy lists in C#. I will list some changes/ additions I made below:
I first changed the names next and value from Wes’s example to head and tail since I feel that makes more sense.
I then modified the stream class to implement IEnumerable<T>. This will allow the stream to be used more seamlessly in code and act just as any other collection does. The implementation for GetEnumerator was pretty straight forward:
The third change I made to the Stream class was to not use his Memoize method but to add another field in the stream class called realized which will store the value of tail after its first use. I felt this was easier to read.
Lazy List Functions
In Wes’s post he shows how to implement Zip (well he actually showed ZipWith but called it Zip) and Map on the stream class. I added to this by first converting them to extension methods just for convenience. Then I added many other methods over lists that any functional programmer is used to having like:
Then to top it all off I added an extension method AsStream to allow you as convert any IEnumerable into a Stream:
With all of these extension methods you will now be able to take a running start at Lazy Lists in C#.
The code contains several examples of using the stream class and also a lazy prime generator which I will discuss in my next post.