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 = ["https://bing.com"; "https://microsoft.com";

"https://msdn.com"; "https://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.

Namaste!

Comments

  • Anonymous
    October 10, 2009
    1st :D Soma, pls fix the code examples. It would be great if one could just copy/paste them in VS. Async example doesn't have the full name of classes specified. Does one still use 'open' keyword to import namespaces? And the ray tracer code in text would be nice. BTW when is VS beta 2 coming out? And what is the current estimate for RTM date?

  • Anonymous
    October 10, 2009
    OK, this took just a few hours, but now I have the 2nd example working. Fsharp PowerPack from CodePlex is required. The compiler gives a few warnings. #light open System open System.IO open System.Net let http url =    async { let req =  WebRequest.Create (url :> string)            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 printfn "html: %a" output_any htmlOfSites Console.ReadKey()

  • Anonymous
    October 10, 2009
    Hi John - That's right, the async example requires the 3 open declarations you mention and the F# PowerPack. The raytracer sample is similar to the F# Raytracer in the Parallel Samples for .Net 4.0 at http://code.msdn.microsoft.com/ParExtSamples. More F# samples also at http://fsharp.net.  

  • Anonymous
    October 12, 2009
    It is nice to see F# moving along and being included with the other main stream languages. What I would like see is how to integrate it with other languages. A Visual Studio solution with a C# project referencing a F# project. Correct me if I'm wrong but I see F# as a complementary language. F# is a language to handle complex math easier than other languages but not to build a complete application with. One would use F# to build a library of intense math function. These math functions in an assembly would be referenced and used in a WPF, XNA, or Silverlight application written in C# or VB. If that is the case are there any examples showing that type of integration?  I see the ray tracing application but it looks to be bolted to Visual Studio. How could I build a Windows Form or WPF application to drive the ray tracing logic via C#? Maybe a game demo built with XNA using an F# library for the physic logic?

  • Anonymous
    October 12, 2009
    Hi John, Stay tuned for news about VS 2010 Beta2 and beyond later this month. -somasegar

  • Anonymous
    October 13, 2009
    SomeONe: For the most part you can expose classes and interfaces in your F# project and instantiate them, call their methods, subclass them etc. from C# like any other .NET class library.  If you expose types based on F#-specific features like pattern matching, workflows or units of measure, you can still use them from other languages but you'll have to know a bit about how those features are implemented and you won't get the benefits of them.

  • Anonymous
    October 13, 2009
    The initial release of F# will (as far as I know) not be quite on a par with C# or VB.NET.  You don't automatically get it in th GAC, so in some deployment scenarios you have extra work to do. My guess is that a lot of people are going to try the waters and really like them.  The code can, in some cases, just be so clean, lean and right that it will become the language of choice for some. I'm really hoping that in a fairly short time it will become fully first class and just be installed everywhere like the main languages currently are.

  • Anonymous
    October 13, 2009
    Congratulations to Don Syme, and all those involved at Microsoft and in the community, in the brillant work to bring F# to this point. I'm looking forward to a bright F#UN (read that BIG F# FUN) future. Using VS, F# and .NET to leverage the power of manycore CPUs and GPUs for visualization. The future is bright,now and F#!

  • Anonymous
    October 15, 2009
    The comment has been removed

  • Anonymous
    October 15, 2009
    SomeOne (sic): Your comment about first class misses what I'm saying. In the narrow meaning I'm using here it becomes first class when it get's automatically distributed the same way that C# and VB.NET are "just there".

  • Anonymous
    October 16, 2009
    The comment has been removed

  • Anonymous
    October 16, 2009
    Now on to a bigger challenge QuotationsVisualizer. There seems to be a lot of code to write and handle GUI stuff. That is not math related and looks to me cumbersome in F#. I get complaints that at times C# is hard to read and interpret into English statement. Even with form designers and other GUI tools. I would hate for those people to try and read these: member this.OpenFsAssembly () =   let opf = new OpenFileDialog()   opf.Filter <- "F# assemblies|.dll;.exe";   match opf.ShowDialog(this) with     | DialogResult.OK ->         ResolveAssemblyDefinitions(opf.FileName)           ( fun t ->               let nd = new TreeNode(t.Name, 1, 1)               ignore(exprTree.Nodes.Add(nd))               nd )           ( fun ndType mi exp ->               let fn = new Text.StringBuilder()               ignore(fn.AppendFormat("Loaded from assembly: {0}rn", mi.DeclaringType.Assembly.FullName));               ignore(fn.Append("Declared in type: "));               if (mi.DeclaringType.Namespace <> null) then ignore(fn.AppendFormat("{0}.", mi.DeclaringType.Namespace))               ignore(fn.Append(mi.DeclaringType.Name));               let nd = this.AddQuotationNode mi.Name (fn.ToString()) exp               ignore(ndType.Nodes.Add(nd)) )               | _ -> () or this to build a Tree and assign properies and event handler:   exprTree.Left <- 32   exprTree.Width <- 200   exprTree.Height <- 268   exprTree.Top <- 288   exprTree.HideSelection <- false   exprTree.AfterSelect.Add(fun e ->     if (e.Node.Tag <> null) then       this.SelectPanel(e.Node.Tag :?> QuotationPanel))

  • Anonymous
    October 20, 2009
    Hi There, This is very cool feature. Thanks for letting us know this. Thanks, Thani

  • Anonymous
    October 22, 2009
    Will there be a F# Express product?

  • Anonymous
    October 22, 2009
    CA Reed - There are no plans at this time for an F# Express with VS2010.  There will definitely continue to be free development tools for F# available though.

  • Anonymous
    October 28, 2009
    Created a Class Library (dll) in F# using VS 08 with the F# CTP October release. Tried using the Assembly from VS2010 beta 1, after referencing the library (dll file generated by the F# Compiler) I can access the Namespaces in the library, but these namespaces vanish once I try to build the project and I can't henceforth access the referenced F# library. It used to work with assemblies I built with the F# May CTP release, this problem started after i installed the October CTP What could be wrong. Thanks in advance.

  • Anonymous
    October 29, 2009
    Hi Adenegan - Each F# CTP releases is matched with a Beta release of VS2010.  The October F# CTP can build libraries which can be be used in VS2010 Beta2.  If you still need to work with VS2010 Beta1, you'll want to contiue using the matching F# May CTP.  If you can, I'd encourage you to move to VS2010 Beta2, which has many improvements over the Beta1 release.

  • Anonymous
    November 27, 2009
    The comment has been removed

  • Anonymous
    November 29, 2009
    The comment has been removed

  • Anonymous
    November 30, 2009
    The comment has been removed

  • Anonymous
    January 16, 2010
    As an Electrical Engineer now into Microsoft .NET development, I instantly saw the potential for F#. I just left a job where F# would make a perfect fit to replace their old, large, monolithic and extremely confusing FORTRAN library.

  • Anonymous
    March 30, 2010
    hola quiero saber como graficar funciones en 3d en f# utilizando directx lpedraza1@cuc.edu.co gracias. hi I know how to plot functions in 3d in f # using directx lpedraza1@cuc.edu.co thanks.

  • Anonymous
    March 30, 2010
    Hi Luis - The F# Samples contain some example code for polotting functions in 3D with DirectX interactively with F#.  See: http://code.msdn.microsoft.com/fsharpsamples

  • Anonymous
    April 08, 2010
    This is interesting news, looking forward for opportunities in VB to F# conversions etc.

  • Anonymous
    April 22, 2010
    This is a simple application that gives you some idea of what I'm looking for. Step 1:  My F# app asks the users to supply values for   variables X and Y.  Ex: X=1 and Y=2. Step 2.  My app asks the user at RUNTIME  what he wants to do with the data.  He writes F# script: for  X=X*Y and Y=Y-1.  It can be anything and can be complex.    Just any valid F# script.   ( This part gives the business user power to define computation at runtime.  ) Step 3.  My app takes  the script and processes it.  Finally, the app writes value of Z to the screen. Do you have some sample like this?  Or is this possible?  Thanks! QishenHuang@yahoo.com

  • Anonymous
    July 06, 2010
    Oh SWEET! This F# language looks awesome! I'm totally going to use this...

  • Anonymous
    August 30, 2010
    The comment has been removed

  • Anonymous
    November 02, 2010
    Hi Soma, I got an site where I found how to generate .net document. I think it would be very much useful for us. when I tried for document generation system, I found a way to call it from .net, for this I found a way from this [http://www.dotnetdocgen.info/] site. It has basic info on all the vendors. It makes for a great starting point.

  • Anonymous
    December 16, 2010
    Great News.

  • Anonymous
    July 11, 2011
    Can I get vs 2010 f# to talk to common lisp?

  • Anonymous
    July 12, 2011
    Hi Ron, We don’t explicitly provide support for interacting with Common Lisp, though an open-source .NET to CL bridge such as RDNZL (http://weitz.de/rdnzl/) may be what you're looking for.  Also, many commercial Common Lisp implementations provide mechanisms for static compilation to COM libraries, which would provide a bridging mechanism between CL and any .NET language. Thanks, Joe Pamer Dev Lead - F#

  • Anonymous
    October 14, 2011
    Where can I download the F# language

  • Anonymous
    October 14, 2011
    @Boy: thanks for your interest!  There is more information on F# available on our team blog (blogs.msdn.com/.../fsharpteam). I recommend starting with the Visual Studio 2011 Developer Preview (go.microsoft.com/fwlink). See this post (blogs.msdn.com/.../f-3-0-developer-preview-now-available.aspx) for more information on what's new.