MSDN UK Team blog

Get all the latest Microsoft developer news, tools, downloads, tutorials and tips right here, every day.
Search Form
Unfiltered HTML
Developers code with microsoft

A Brief Introduction to F#

Unfiltered HTML
Unfiltered HTML

A Brief Introduction to F#

  • Comments 5

Our guest writer for this fortnight’s MSDN Flash is Mark Bloodworth, an Architect Evangelist at Microsoft UK, who kindly offered to write a brief introduction to F#. Mark’s interests extend to all those “other” languages like Ruby, Python and F# so we should be in safe hands. Over to Mark…

When you create a new project in Visual Studio 2010, you may have noticed there’s an option to create a library or an application in F#. If you’ve ever wondered what F# is all about but not had the chance to find out, read on.

Simply put, F# puts the focus on what you want to achieve rather than how you want to achieve it. F# is a multi-paradigm programming language – it supports object-oriented, functional and imperative programming styles – with a succinct and expressive syntax. It is also a statically typed language that supports type inference, (ie the compiler can deduce data types without you having to declare them). F# is a first class .NET language; it builds on the .NET framework you already know.

F# has a couple of tricks up its sleeve that make it easier to get started. It comes with an interactive console that you can access in Visual Studio (View -> Other Windows -> F# Interactive). In addition to typing F# statements and executing them directly in the console, you can also highlight lines in the code editor and send them to the interactive console.

With its roots in functional programming, F# treats functions as values, which makes it easy to code higher order functions as well as compose functions. A simple example is to supply fewer than the required number of arguments, creating a new function that expects the remaining arguments (a technique referred to as currying.) Here’s an example:


The first line creates a function called multiply that takes two integers and returns their product. The second takes one integer and calls the multiply function with that integer and 2, thus creating a function that doubles the value passed in. The last line prints the output. In Visual Studio 2010 you can highlight these lines and send them to the Interactive Console to see the result (if you copy & paste to the console, type ;; to evaluate the expressions).

You can also easily create anonymous functions with the fun keyword as in the following example:

image calls the function specified on each value in a given list. The result of running this code is a list of the values from 1 to 10 doubled.

Another aspect of functional programming is immutability. Once you have assigned a value it cannot be changed, so the following code won’t compile:


Immutability helps to prevent certain types of bugs and lends itself to parallelisation given it is inherently thread safe. (F# has the mutable keyword if you need to be able to change values). For a discussion on the subject of immutability see these two articles.

A final thing to note in this brief introduction is that whitespace is significant in F#. Indentation is used to signify nesting. Here’s a function that multiplies the input by 14 (with a line to show the function working):


And here’s another version of the same function with a body:


Note that the indented lines are part of the function and that the result of evaluating the last indented line is implicitly returned as the result of the function.

There is far more to F# than I have covered here, so if you are interested in learning more head to the home page and dive in.

You can find a great list of learning resources on Brian McNamara’s blog or watch “The Future of F#” video from PDC10. There’s also a F#unctional Londoners Meetup Group if you live in the London area.


Mark is an Architect Evangelist at Microsoft UK

He blogs at:

He Twitters at:

  • Is

      let double x = multiply 2 x

    really an example for "currying"? Shouldn't it be

      let double = multiply 2


  • Hi,

    The immutability example is a bit misleading. The code will compile unless used in a module to declare two bindings with the same name.

    Following function:

    let f () =

       let x = 1

       let x = x + 1


    is perfectly valid, compiles and returns 2 as its result.

    iouris at gmail dot com

  • Mark, in your currying example, there's no need to supply the 'x':

    > let m a b = a * b;;

    val m : int -> int -> int

    > let square = m 2;;

    val square : (int -> int)

    > square 3;;

    val it : int = 6


  • Mike, you're quite right.  The x can be omitted.

    iouri - I wanted to include the concept of immutability and maybe the example is too simple..  As I understand it, identifiers in functions can be redefined (as in your example) although I think this may be at the cost of reability.  I also think that for newcomer to F#, there is a benefit in adopting a functional style (as aopposed to continuing to use an imperative style.)

  • Nice post Mark. I needed that overview. Examples are the key to getting these things through my thick skull.


Page 1 of 1 (5 items)
Leave a Comment
  • Please add 1 and 6 and type the answer here:
  • Post