Page 2 of 4
Enough of the theory, it’s time to see how it all works in F#. You can download, and find other information about F# from: http://msdn.microsoft.com/fsharp/
As already mentioned F# is included as part of Visual Studio 2010 and this is the version that is used to illustrate this article. You can also download a standalone version that installs under Visual Studio 2008 or the Visual Studio Shell. The VS Shell is a cut-down IDE that can be used, free of charge, to host other languages and development tools without the need to install the full version of Visual Studio.
If you want to use the VS Shell then download and install the Integrated version runtime from:
The download that you need is called
“Visual Studio 2008 Shell (integrated mode) with Service Pack 1 Redistributable Package” and you need to unpack it and then use the installation program. Do this before you install F# and you will discover that you have a choice of F# projects to work with. Notice that none of the Express versions of Visual Studio can be used with F# and there is currently no Express F#.
Once you have F# installed and working within Visual Studio – full version or Shell – you can create a new F# Application Project.
This has a single F# source file. Let’s start off with the traditional “Hello World” program:
printfn "Hello F# World"
The open command simply loads a reference to a .NET library and it is needed if we want to use the Console object. The printfn function prints the string on the console and the ReadKey function waits until the user hits a key, any key.
Hello World – but not a good example of functional programming
If you type this in and run it you will be rewarded with the traditional message. However, in this case the Hello World example really isn’t very good. It’s a terrible example of a functional program as the printfn and ReadKey functions are being used for their side effects on the user interface.
So with this said let’s move on quickly to a more functional example.
It is tempting to present examples where functional programming seems most at home – i.e. computing with a mathematical flavour – but there are lots of such examples already available so let’s try and keep in the realm of general programming tasks.
First we need to look a little more closely at the idea of a function. Declaring new “variables” is easy:
This creates two strongly typed immutable variables – once assigned a value you can’t change it.
To define a simple function that adds two values together we can write:
let add x y = x + y
The first occurrence of x and y define the parameters for the function and what happens to the parameters is given by the expression to the right of the equals sign.
In a functional language parameters like x and y are the only things that are “mutable” in the sense that they can be repeatedly given values each time the function is called. This idea of the parameter being the only mutable thing in functional programming is key to the subject of lambda calculus from which derives the term “lambda expression”.
In lambda calculus mutable parameters are identified by writing a lambda in front of them - but we just identify them as parameters.
Using the function is also easy:
let k=add i j
and you can check that things are added together correctly using:
printfn "%i %i %i" i j k
If you know C, C++ or C# the use of the formatting string in the printfn function will be familiar – it simply means print three formatted integers.