Calculating with infinite sequences on MSDN
About a year ago, I wrote an article about using lazy computations in C# 3.0. It was published by the C# Community PM Charlie Calvert at the C# Developer Center. The article was a first of two articles where I wanted to demonstrate that C# 3.0 can be used for implementing useful constructs known from functional languages. I realized that I never posted the link to the second article to my blog, so you can find the annotation and link below.
However, I remembered about these two articles because I was just working on chapters 11 and 12 of the Real-world Functional Programming in .NET book that I’m writing. Lazy values, which were the topic of my first article, are discussed in the second part of chapter 11 and IEnumerable and F# sequences are the topic for the first part of chapter 12. Because I already wrote two articles on this topic, I had to think really hard to find better (and still simple enough) examples where these concepts are useful in practice. I also finally have enough space to show how these two concepts relate and talk about some interesting background – for example in Haskell, lazy sequences are in fact just ordinary lists that are lazy thanks to the Haskell nature.
A year ago, I definitely wouldn’t believe that today, I’ll be writing about the same topics, but this time as part of a book that has partly the same goal as these two articles – to show that functional programming ideas are really useful in the real-world and can enrich your programming toolbox (no matter whether you’re using C# or F# language). Anyway, here is the link to the second article – as usual when I look at something that I worked on a long time ago, I think I should rewrite it to make it better :-), but it still gives you an idea what is the book that I’m working on about:
My previous article explored lazy evaluation and looked at how it can be simulated in C#. We implemented a class
Lazy<T>, which represents a value that can be evaluated on demand—this means that the class "knows" how to calculate the result, but doesn't actually calculate it until it is really needed in the program. One very interesting data structure known from functional programming that can be implemented using this lazy cell is a lazy list. Each element in a lazy list stores the value associated with the element (for example a number) and knows how to calculate the next element in the list, but it is lazy meaning that the next element is not calculated until its value is accessed by the program. The most interesting aspect of lazy lists is that they can be used for representing infinite sequences—this is possible because the elements are calculated only when needed and so the list will always store only a finite number of elements, but it will still be able to calculate the next element if it is needed.
In this article I will show that lazy lists can be implemented using the
IEnumerable<T> type and I will also demonstrate how LINQ query operators can be used to manipulate infinite lists. Finally, we will look at an implementation of the Mandelbrot set program (you can see it on the screenshot) using the techniques described in this article.
You can read the complete article here: Calculating with Infinite Sequences in C# - C# Developer Center
By the way, I apologize for writing about the book all the time :-), but I hope you can understand that! I just "redirected" all my writing activities to the book, so the blog has been a little bit quiet and when I find the time to write something, it is usually inspired by working on the book, so I have to reference it… But I promise that the next couple of articles will be technical and I believe quite interesting!
Published: Thursday, 13 November 2008, 2:36 AM
Author: Tomas Petricek
Typos: Send me a pull request!
Tags: c#, functional, universe, writing, links