F# – a functional programming language, in more ways than one

[Another in my continuing series on the innovative Microsoft Research projects I had a chance to check out in Cambridge…]

Don, Ralf and Thore at The Eagle I had the good fortune of meeting Don Syme at Microsoft Research in Cambridge, who introduced me to his brainchild, F# (which is pronouced “F Sharp,” and that’s Don on the left of this photo.) 

F# is an amazingly powerful functional programming language with a syntax and features partially inspired by the functional language OCaml.  It includes deep interoperability with .NET, and even tools support with AutoComplete and code colouring in Visual Studio 2005!

In a functional programming language like F#, a program is composed of a series of bindings of expressions to identifiers.  A purely functional program does not use any mutation: rather than modifying state to produce values, it constructs new values without overwriting existing values.  The Wikipedia entry in functional programming goes into more detail.

But the “why” of functional programming is just as important as the “what.”  I definitely had my eureka moment while Don was walking me through F#, by way of an impromptu riff on some of his F# “getting started” materials.  Not only are the F# language and environment ideal for prototyping, interactive scripting (think Python) and interactive visualization (think Matlab), but there are also some abstractions and concepts that are much more easily described in a functional language like F#.

Awesome F Sharp Demo There’s an awesome F# demo (pictured here) that you can step through yourself, where F# taps into Managed DirectX to display a graph and other 3D information in real-time.  You can visualize the results, even change them using the immediate window, and use your mouse to navigate the interactive display.  Absolutely killer demo.

The demo is possible because F# offers deep, two-way interoperability with the .NET languages.  Assemblies that are written in C#, VB.NET and other .NET languages can be directly accessed from F#.  (For example, here we’re accessing the Managed DirectX libraries.)  And, going the other way, F# data and code can also be directly accessed from C# and other .NET languages.

In addition to F#’s command-line interactive tool, the Visual Studio Extensions for F# offers an add-in which produces an “F# Interactive” window in Visual Studio 2005, from which you can execute code.  (Visual Studio 2003 support is also available, but I haven’t tested it.)  The one sad note is that because it’s an Add-In, the Visual Studio Extensions for F# don’t work with the Express Editions of Visual Studio.

If you’re a developer, functional programming almost certainly has influenced languages that are close to you.  In addition to C# (and its up-and-coming LINQ extensions), languages like Perl, Python, and Ruby all offer features such as lambda functions, higher-order functions, list comprehensions and other constructs that have been borrowed from functional programming.

So if any of this piques your interest at all, you should definitely check out F# and run through the samples!  My thanks again to Don for such a great demo and for influencing my thinking on functional programming.

Here are all the links you need to download F#, get going, and connect with the community:

Home of F# at Microsoft Research: http://research.microsoft.com/fsharp

Don Syme’s Blog: http://blogs.msdn.com/dsyme  

Community site for F#: http://cs.hubfs.net

The Managed DirectX 3D Simulation and Visualization sample: http://cs.hubfs.net/forums/thread/95.aspx

Direct download of 3D Code Demo: http://cs.hubfs.net/blogs/thepopeofthehub/attachment/101.ashx

Note: I am using the April 2006 DirectX SDK.  I had to update the #I include information at the top of the 3D Demo source file as follows.  Hope this helps!

#I @”C:\WINDOWS\Microsoft.NET\DirectX for Managed Code\1.0.2904.0″ ;;
#I @”C:\WINDOWS\Microsoft.NET\DirectX for Managed Code\1.0.2902.0″ ;;
#r @”Microsoft.DirectX.dll”;;
#r @”Microsoft.DirectX.Direct3D.dll” ;;
#r @”Microsoft.DirectX.Direct3Dx.dll” ;;


Comments (6)

  1. I’d heard a bit of talk about F# but I’d always ignored it.  Coming from a background in Physics and using computers for mathmatical modelling I had assumed that F# was a .NET implementation of Fortran – Dope!

    Great intro – I’m definately going to go take a look.

  2. Rob Burke has just written a blog entry on F#:  It was great to have Rob to visit – I first…