F# Webcast (IV.) - Developing standard .NET libraries
In the previous parts of this webcast series we've developed an F# script that downloads RSS feeds
asynchronously and in parallel and searches them for the specified keywords. We followed the usual F#
development style, so after introducing the basic functional
concepts, we wrote the code in the simples possible style
and demonstrated how to use System.Xml and System.Net namespaces. Then we
refactored the existing code, to run asynchronously and process
the results potentially in parallel, which was very easy thanks to F# asynchronous workflows.
In this part of the series, we'll make the next evolutionary step of our sample application. We'll turn the code that originally used F# tuples and lists into code that uses standard .NET objects and we'll also see how to declare a class in F#. This simple modification will turn the script into an F# library that is almost indistinguishable from a library developed in C#. We'll also look how you can use the library from C# web application to show the interop between C# and F# in practice. We'll start with the code from the previous part, so if you missed that, you may want to check it out or download the source code.
Read the complete article (English)
Monday, June 15, 2009
F# Webcast (III.) - Using Asynchronous Workflows
In this webcast, we'll look at improving the code for downloading and
processing RSS feeds that I presented in the second part
(if you didn't see earlier parts, the first one was an introduction to
basic functional ideas). The previous part demonstrated how to use .NET libraries
and we implemented a simple downloadUrl function for obtaining content
of from the web and we've also seen how to load the data into an XML document object
and how to filter items. In this part, we'll modify the code to run asynchronously and
potentially in parallel. To use some of the functionality, you'll need to get
FSharp.PowerPack.dll, which is available with the VS 2008 installation or
as a separated download for VS 2010 [4].
Now that we have the first version of the code, we can start refactoring it. I'm using the term in a slightly vague meaning - we're of course going to change the behavior of the code. We'll wrap it into F# asynchronous workflow to run without blocking threads and we'll also run multiple downloads in parallel. However, this can still be viewed as refactoring in some sense, because we're not changing the core behavior of the code. As you can see from the webcast, these kinds of refactorings are also very nicely supported by F# syntax...
Read the complete article (English)
Friday, June 05, 2009
F# Webcast (II.) - Using .NET libraries
About a week ago I posted the first part of my F# webcast series. It focused on explainining the basic ideas behind functional programming such as immutability, recursion and passing functions as arguments to other functions (or methods in C#). In the first part, we've seen some C# code to demonstrate the ideas and also a bit of F#, mainly to show the basic language features.
The second part is going to be exclusively about F#. It'll demonstrate how we can start writing
a demo application that grabs data from RSS feeds and processes them. You'll learn how to access
.NET libraries from F# (in particular, we'll use System.Net and System.Xml).
We'll develop the code iteratively, which means that we'll start by simply enumerating the RSS elements using for loop and printing the results and then we'll refactor the code to use tuples and sequence expressions to turn it into processing code that generates a sequence of feed items. Finally we'll also demonstrate how to use some of the functions from the previous part such as List.filter in practice.
Read the complete article (English)
Monday, June 01, 2009
F# Webcast (I.) - Introducing functional concepts
Now that Visual Studio 2010 Beta 1 is out, it is finally a good time to take a look at one of the (in my opinion) most interesting new features in the new release - the F# language. F# existed for quite a long time now as Microsoft Research project, but is now becoming a real Microsoft product. Interestingly, F# is still available as a plugin for Visual Studio 2008, so if you want to try it you don't have to install the whole new beta of 2010.
There are already many resources for learning F# including my functional programming overview, which is a Manning Greenpaper for the book Functional Programming for the Real World that I'm writing with Jon Skeet and my four-part F# introduction. There are also some useful links on the official F# web site including some talk recordings. However, I haven't yet seen any good F# webcast focusing mainly on showing F# source code, starting from simple functional concepts to the real-world features like asynchronous workflows and object-oriented programming in F#, so I decided to create one.
So, here it is...
Read the complete article (English)
Monday, May 25, 2009
Internship project: Reactive pattern matching
I already mentioned that I was doing my second internship with Don Syme at Microsoft Research in Cambridge. This time, I was in Cambridge for 6 months from October until April, so it has been more than a month since I left, but as you can guess I didn't have time to write anything about the internship until now... There isn't much to say though, because the internship was simply fantastic. Cambridge is a beautiful place (here are some autumn and winter photos), the Microsoft Research lab in Cambridge is full of smart people, so it is a perferct working environment (except that you realize that you're not as clever as you think :-)). Also, it is just a few meters away from the Computer Laboratory of the Cambridge University, so there are always many interesting talks and seminars. So, big thanks to Don Syme, James Margetson and everyone else who I had a chance to work with during the internship.
One of the reasons why I didn't have much time to write about the internship earlier is that I was invited to the Lang.NET Symposium shortly after the end of the internship. I had a chance to talk about my project there as well and there is even a video recording from the talk (the link is below), so you can watch it to find out more about my recent F# work.
Read the complete article (English)
Sunday, May 17, 2009
Imperative computation in F# (II.) - Writing break and continue
As I already wrote in the first part of this series,
the F# language doesn't support some of the language constructs known from imperative
languages such as C#. In particular, we cannot use imperative return statement
that returns the result of a function from any place in the function code. In functional languages,
every construct is an expression, so to get the overall result of the function, the
F# language evaluates the expression and the value of the expression is used as the result.
In the previous article, we've seen that we can simulate this construct in the F# language
using F# computation expressions and I showed how to implement computation named imperative
that allows us to write for example the exists function for working with sequences
like this:
let exists f inp = imperative { for v in inp do if f(v) then return true return false }
In this article, we're going to look at two more imperative constructs and we're going
to talk about break and continue. We'll see that we can quite
easily extend the computation builder from the previous article to allow writing code
that is syntactically very close to what you would write in C#. As I already mentioned,
there are of course some performance overheads when using computation expressions, but
I find it very interesting how nice imperative syntax we can get in functional F#:
imperative {
for x in 1 .. 10 do
if (x % 3 = 0) then do! continue
printfn "number = %d" x }
The only difference between this code and the code we'd probably write if F# supported
continue as a keyword is that we need to wrap the code inside the imperative
computation and that we need to add the do! primitive before the continue
value. Now that we've seen an example of using the continue value inside
the imperative computations, let's look how we can extend the computation builder from
the previous article to add this feature...
Read the complete article (English)
Saturday, April 25, 2009
Imperative computation in F# (I.) - Returning results from a function
One of the limitations of F# is that it doesn't very well support some of the
advanced imperative language constructs such as break, continue
or imperative style of returning value from a function, meaning that you can't write
something like return false in the middle of the function. This has
good reasons. F# doesn't in principle have the notion of currently executing statement
and instead treat every code you write as an expression. Clearly, when there is no
current statement, we cannot jump to other statements. If you're looking
for more information about these basic principles, you can take a look at my book
Real World Functional
Programming, which covers this distinction in details in chapter 2, but we'll look
at a brief example that will clarify this idea shortly.
Often, there is really no need to use break or other imperative constructs
in F#, because you can write the same thing more elegantly using one of the provided higher
order function such as Seq.exists or Seq.tryfind. However,
there are still some cases where the imperative programming style makes it easier
to express our original intention. Also, implementing your own higher order
functions (akin to Seq.exists) would sometimes be much easier if we
could just use imperative return.
So, what can be done about this?
Read the complete article (English)
Thursday, March 19, 2009
Real World Functional Programming: Second review finished!
I've been working on the Real World Functional Programming in .NET book for quite some time now. In fact, I had the first discussions with Michael Stephens from Manning in March last year and I started thinking about the book at that time, so it has been occupying my mind for almost a year now! Until recently, I was feeling that we're not getting much closer to the end of this project, because writing a book is just a lot of work. However, I think I can finally see that we're getting closer to actually finishing the book. At Manning, we've recently finished the second review, which means that I've just got another set of very useful comments - a big thanks to all the reviewers! I'm also getting close to finishing the first draft of the whole manuscript (depending on the reviews, the content may still change a bit, but I expect to write at most one new chapter from now). Hopefully, the drafts will soon make it to the MEAP release of the book.
Read the complete article (English)
Monday, March 02, 2009
Source code for Real World Functional Programming available!
As you can see, there has been quite a bit of silence on this blog for a while. There are two reasons for that - the first is that I'm still working on the book Real World Functional Programming, so all my writing activities are fully dedicated to the book. The second reason is that I'm having a great time doing an internship in the Programming Principles and Tools group at Microsoft Research in Cambridge with the F# team and namely the F# language designer Don Syme. The photo on the left side is the entrance gate to the Trinity College of the Cambridge University taken during the few days when there was a snow. I recently started using Live Gallery, so you can find more photos from Cambridge in my online gallery. Anyway, I just wanted to post a quick update with some information (and downloads!) related to the book...
Read the complete article (English)
Thursday, February 12, 2009
Functional Programming in .NET using C# and F# (Manning Greenpaper)
Functional programming languages have been around for a while and were always astonishing for their ability to express the ideas in a succinct, declarative way allowing the developer to focus on the essence of problem rather than on technical details of the solution. Recently, functional paradigm is gaining new prominence as an efficient way to handle development of multi-processor, parallel and asynchronous applications.
Functional ideas are arising in C# as well as in other main-stream languages and functional languages are becoming an alternative for real-world projects. Also, Microsoft recently introduced a new language called F#, which has a strong background in traditional functional languages, but as a .NET language also benefits from the rich .NET and Visual Studio ecosystem.

Available via MEAP | 500 pages
Softbound print: March 2009 (est.)
This article is partially an excerpt from my book Real-world Functional Programming in .NET [1]. Thanks to my editors at Manning I have the permission to publish it on my blog. We’ll look at several aspects of functional programming and how the same concepts, which are essential for the functional paradigm, look in the F# and in C# 3.0 with LINQ. We will shortly look at the basic programming language features like lambda functions and type inference that are now available in both F# and C#. Functional programming isn’t only about language features, but also about using different programming style, so we’ll look at some high level concepts as well. These include using immutable data structures for developing code that can be executed in parallel and writing code in a more declarative style.
Thanks to the combination of C# 3.0 and F#, this article shows the ideas in a way that should be familiar to you in C#, but also shows a further step that you can take with a primarilly functional language F#. If you're a .NET developer and you want to understand what functional programming is and how it can help you to become better and more productive then continue reading. If you'll find this article interesting, then don't forget to check out the book, which explains everything in larger detail and discusses many other interesting ideas.
Read the complete article (English)
Thursday, December 11, 2008


