F# in VS2010

With Visual Studio, we strive to give your organization the tools to tackle a broad range of software problems with the interoperability and efficiency that you need and have come to expect from software based on the .NET Framework.

As part of this, Visual Studio 2010 marks the first release to directly support functional programming through the F# programming language.

F# is a highly productive .NET programming language combining functional programming and object-oriented programming, and is ideally suited for parallel, algorithmic, technical and explorative development. F# is the result of a close partnership between Microsoft Research and the Visual Studio team, and since announcing F# in Visual Studio 2010 we’ve seen a surge of interest and uptake in the language. We’ve also worked closely with the F# community and major adopters to ensure it meets the needs of professional software developers working in these domains.

F# brings many new features to Visual Studio 2010, covering everything from “programming in the small” with Tuples and functions to simple, error-free asynchronous programming and strong types for floating point code. Below are a few of the highlights of this addition to the Visual Studio languages.

Simple, Succinct Syntax

F# is a strongly-typed language like C#, but with a lightweight syntax often seen in a dynamic language like Python. This gives your F# programs a lightweight, math-like feel.

let data = (1,2,3)

let rotations (x, y, z) =

[ (x, y, z);

(z, x, y);

(y, z, x) ]

let derivative f x =

let p1 = f (x - 0.05)

let p2 = f (x + 0.05)

(p2 - p1) / 0.1

let f x = 2.0*x*x - 6.0*x + 3.0

let df = derivative f

System.Console.WriteLine("The derivative of f at x=4 is {0}", df 4.0)

When run, this program will print: “The derivative of f at x=4 is 10”

Parallel and Asynchronous Programming

.NET Framework 4 and Visual Studio 2010 contain great libraries and tools for easy parallel application development. F# complements this with language features designed to make parallel and asynchronous programming more intuitive. This includes fundamental language features like immutability and first-class functions, and powerful programming models, such as asynchronous workflows, which let you write asynchronous code in the same linear style as the synchronous code you are used to.

let http url =

async { let req = WebRequest.Create(Uri url)

let! resp = req.AsyncGetResponse()

let stream = resp.GetResponseStream()

let reader = new StreamReader(stream)

let! contents = reader.AsyncReadToEnd()

return contents }

let sites = ["http://bing.com"; "http://microsoft.com";

"http://msdn.com"; "http://msnbc.com"]

let htmlOfSites =

Async.Parallel [for site in sites -> http(site)]

|> Async.RunSynchronously

Integrated with Visual Studio 2010 and .NET 4

F#’s integration in Visual Studio 2010 includes project templates, IDE support, IntelliSense and integration of the F# Interactive tool window. F# can be used to develop applications and components targeting .NET 2.0 through .NET 4 and Silverlight. As a .NET language, F# can be used comfortably alongside C# and Visual Basic.NET. And in .NET 4, core types that F# uses, such as Tuple, Lazy, and BigInteger, are now part of the .NET Framework and can be used across all .NET languages.

The F# Interactive tool window enables an explorative development style within Visual Studio. Below you can see the F# source code for an F# script open in the Visual Studio editor and the F# Interactive tool window where code is executed interactively. At the top right is the Form and graphics that the script has created.


F# Interactive tool window


Units of Measure

One ground-breaking feature of F# is Units of Measure, which allows you to annotate your floating point code with units, such as meters and seconds. This is simple to do, and errors will be reported during development when code combines units incorrectly. This provides compile-time checking of the correctness of floating-point code without sacrificing performance.


Units of Measure

That’s a quick look at just a few of the exciting features of F#. For more on F#, visit the F# Development Center on MSDN.