Getting Functional with F#

Today, .NET developers using only Microsoft's solutions have the chance to use Visual Basic, C#, and C++ to develop their applications. All three of these languages are based on a programming paradigm (model) called imperative programming where you as a developer specify the exact steps to solve the problem at hand. This is done by using statements in the correct order, but also with the help of variables and instances of classes to store the state of the application.

Although this model of programming is very useful in many situations (outside the domain of programming, furniture assembly instructions and cooking recipes are also examples of imperative programming), it is not the best method for every task. This is the reason functional programming was developed.

Functional programming is closer to the mathematics you learned at school than languages such as C#. However, this is not to say that functional languages are useful only for calculations. In fact, you can do all the same things in functional languages such as F# as you can do with C#. Only the way you do it differs.

In functional languages, the main question is what kind of data you need, and what should be done with the data. Furthermore, in functional languages, the concept of a function is fundamental. As you will soon learn, this also means that maintaining state is not important anymore. Contemporary examples of functional programming languages include XSLT, LINQ, and of course, LISP. For .NET developers, F# is the new kid in town.

Introducing the F# Language

F# started as a research project of Microsoft Research, and since then has grown to become the most probable candidate of being the first functional language to be included with Visual Studio. Although the term functional might in your mind be somewhat a limiting factor, the good thing about F# is that it supports object-oriented constructs, and also has full access to the .NET Framework.

If you are familiar with languages such as C#, you might at first find that F# is at the same time both familiar and unfamiliar to you. For instance, if you wanted to print out an array of numbers multiplied by two in F#, you could write code like this:

These four lines introduce you two important concepts in F#: defining functions and lists, and standard functions of the language such as List.map. Of course, if you wanted to write the canonical "Hello, World" example, the "printfn" function would be your best friend.

Another typical example of functional languages is the way functions are created and used. Consider the following example to calculate the factorial (factorial is the product of numbers from 2 to n, so that, for example, 5! equals 2*3*4*5 = 120):

In functional languages, recursion is used instead of regular loops, and F# is no exception. In both of the above code listings, the first code line defines a new function with the "let" keyword. In the second example (above), the "rec" word tells F# that the function "factorial" is used recursively. Here is another listing, which finds numbers in an array that are larger than five:

As you can see, functional programming code looks familiar, but at the same time also can get a little used to, especially if you compare it to the rather straightforward manner of many C# applications. Even though functional languages in general are their best in algorithm implementations and in sectors such as financing, statistics, and mathematical research, F# can be used in much broader ways.

Consider, for example, the following code to retrieve the latest USD to Euro exchange rate from the European Central Bank (ECB). To use the code, you need to add a reference to the System.Xml.dll assembly file in your solution:

As you can see from this listing, you can fully utilize the .NET Framework in your F# applications. In fact, you could view F# as a hybrid language, supporting both functional and imperative constructs. The above code would look much the same if it had been written with C#, for instance.

That said, classes, objects, and interfaces can all be used in F#. The language lets you build your own classes and interfaces as well. By the way, note some interesting syntax in the above listing. For instance, long strings can be split into multiple lines with the backslash character.