Share via


F# ...Functional programming in the .Net world - Kevin Pfister

F# was developed as an experimental programming language by Microsoft Research to provide a combination of many sought after features in other programming languages.

The problem with most programming languages is that they usually have to give up on functionality to achieve it elsewhere; C# for example gave up on a slight performance overhead while gaining the benefits of managed code.

The goal for F# was to create a type-inferred functional programming language that provides type safety, succinctness, expressivity, scripting while still maintaining performance relative to a well-supported modern runtime system.

F# is a fully supported .NET language and you can download the CTP from the Microsoft download site. Developed as a pragmatically oriented variant of ML with a core that shares many of the language fundamentals as OCaml, F# can still run near the speed of C++.

The first port of call for anyone wanting to know more about F# would be its developer section on MSDN, I’m now going to go through and explain more technically about F# and how to program in it.

Once you have downloaded and installed the CTP for F# you can now go into visual studio and create a new project in the language.

 FSharp

As F# is based on OCaml it means it has the ability to cross compile OCaml source code, however this also means it can create a syntax that isn’t as elegant as we would hope for, thus we can use the #light directive at the being of our source to simplify the syntax.

FSharp 

The next big thing about F# is Type Inference, if we were to make a function in C# we would have to say what the argument types were and what the return type was, F# however makes these determinations at the compiler stage using the function signature. If we were to make the following declaration of a function in F# that doubles a number given to it.

let doublenum x = x * 2

This would cause the F# to believe the return value would be an int... why? Well the return value is the last executable statement, in this case ‘x * 2’ F# also knows that there is only 1 argument to the function ‘x’. The reason however why it will return an int is because the last statement contains *, although * can be applied to byte, uint64, double etc. F# will default it to a signed 32bit integer for our convenience.

Now we can override the way that F# does this to make some interesting effects by applying type annotation to one or more arguments. We can do this by declaring what type they are, for example.

let concat (x: string) y = x + y

We have stated that x is of type string, this then infers that y must also be a string as + only works when both variables are strings.

Now what if we want to return more than one result? This can be achieved by wrapping the values you want to return in brackets at the end of the function, as shown in the following example.

let wordCount text =

    let words = String.split [' '] text

    let wordSet = Set.of_list words

    let nWords = words.Length

    let nDups = words.Length - wordSet.Count

    (nWords,nDups)

This function returns the number of words in text (nWords) and the number of duplicate words (nDups).

So now you know how to make functions in F#, let’s move into GUI. As F# utilises .Net we can use System.Windows.Forms and from that we can make Windows Forms to display things on. Below shows how easy it is to make a new window in F#. ( Remember you have to reference System.Windows.Forms as part of the project as well)

open System.Windows.Forms;;

let form = new Form(Visible=true,TopMost=true,Text="Hello GUI World");;

Now if you want to add a control to the form you have to add it to the Forms Control property, below is an example of adding a RichTextBox to the form we just made.

let textB = new RichTextBox(Dock=DockStyle.Fill, Text="Hello GUI World");;

form.Controls.Add(textB);;

So in all F# is a very powerful and interesting language which is great if you are moving from the functional world of ML, OCaml etc.

If you want to try out some more samples you can download F# samples at code.msdn which shows a selection of slightly more complex examples for those wanting a challenge.