This article is also available as a TechRepublic download.
F# (pronounced F Sharp) is a functional programming language based upon OCaml. It was developed by Microsoft's Microsoft Research department. All of the tools needed to run it are available for free from Microsoft.
F# runs within the .NET CLR, which means it can be used within existing .NET projects. Additionally, it can make use of any other .NET code. This overcomes a traditional weak spot for functional programming languages, namely a lack of libraries, utilities, and development environments. The F# language can stand alone, but it also plugs directly into Visual Studio. F# is noted for being very efficient in terms of the number of lines of code needed to accomplish particular tasks. It is also reported to run as quickly as C# — and potentially even faster than C# for some tasks.
InstallationTo install F#, you need to download the installation package. Unfortunately, the Microsoft F# Web site appears to be a bit out of date. To get the latest version of the software, go to the Microsoft Research Downloads section and perform a search for the F# Compiler. The most current version is F# 18.104.22.168, released May 1st, 2007. Installation is the same as any other Windows application and is quite easy. When you start Visual Studio, you will now see F# Research Compiler With Visual Studio added to the splash screen (Figure A).
Our first F# projectTo give F# a try, start a new project in Visual Studio. Under the Other Project Types group, you will see F# Projects. There is currently only one template there (Figure B) but it is enough to get us started. Name this new project and click OK.
F# templateYou should now have an empty project. Unlike the standard Windows Forms or Web Application projects, the F# template does not create a lattice work of directories, default files, or anything of the type. Add a new item to the template, choosing F# Source File as the template (Figure C).
F# source fileYou will see that the new file has been added. Opening it shows that the F# template is filled with code that functions as a very basic tutorial. While this article will barely even be able to scratch the surface of F#, we will walk through a simple piece of code: a Fibonacci Sequence generator. This is a fairly standard, basic program for learning functional programming. We are going to use the standard definition of the Fibonacci Sequence. The code for the fib function is from the F# Manual. Here is our short little program (Listing A):
// A Fibonacci Sequence generator
//Get the command line arguement as an integer type (Int32)
let fib_number = int_of_string (System.Environment.GetCommandLineArgs().GetValue(1).ToString());;
//Set the label "fib" to be equal to a Fibonacci Sequence function
let rec fib n =
if n < 2 then 1 else fib (n-2) + fib(n-1);;
//Print the input number and the Fibonacci sequence
Printf.printf "\nThe Fibonacci value of %u is: %u\n" fib_number (fib fib_number);;
//Exit the program, returning a normal status code to the OS
The first line of the program is simply retrieving the second element of the command line arguments array (the first element is the executable name itself), converting it to an integer (F# is statically typed), and saving the value to a label named "fib_number". The second line defines a function with the label "fib". This function outputs the number 1 if the input (n) is less than 2; otherwise, it returns the sum of calling itself (the "rec" attribute marks the function as potentially recursive) with n - 1 and calling itself with n - 2. The third line prints our output using a very familiar printf syntax. Finally, the program exits.
This short little program demonstrates many interesting points of F#, which you will see if you step through the application in Visual Studio (be sure to specify a command line argument for the integer you want the Fibonacci sequence for). The most interesting item of note is the lazy evaluation. Lazy evaluation is the idea that the value of a label is assigned when that label is first used, instead of when the label is defined. In this case, stepping through the code (F11 in Visual Studio) with a watch on "fib_number" shows that although the label "fib_number" has been defined as the first line of the program, it does not get a value until "fib_number" is called by the printf function!
The next item of note in this sample is the use of recursion. By having a recursive function here, the number of lines of code is very small. While this example may be inefficient for large numbers (the stack usage can get rather high), and there are much more efficient ways to write this example in terms of speed, recursion is a technique that can solve many problems rather well.
The final point in this code sample is the ease of accessing the .NET Framework from within F#. The call to System.Environment.GetCommandLineArgs() works precisely as expected. Casting the resulting Object type to a string via the ToString method, and then casting it to an F# Int32 type by way of the int_of_string function does exactly what we would expect it to. While F# is statically typed like C#, Java, and many other compiled languages, it also performs type inference in the manner of languages like Perl.
F# is a rather unusual language within the .NET world. It can be compiled and called (and make calls to) any other code in .NET. At the same time, it also ships with an interactive runtime environment, which functions as a line-by-line interpreter. It has static typing like many compiled languages but also performs type inference like many dynamic and/or interpreted languages. It can be (but is not forced to be) object oriented as well.
The F# documentation itself is still rather sparse; for the sake of writing the short program above, it took me some time to find out how to cast the Object returned in the first line into an integer. That being said, the OCaml language has a large amount of documentation available for it. From the information on the F# Web site, it looks like F# does not stray too far from OCaml, but there are definitely some differences between the two. The Visual Studio integration is a nice bonus as well.
So go ahead and give F# a try! It is free, and you may learn some new coding techniques, even if you decide that F# is not for you.
Justin James is an OutSystems MVP, architect, and developer with expertise in SaaS applications and enterprise applications.