A Brief Introduction to F#

Out guest writer for this issue 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:

image

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

List.map 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:

image

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):

image

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

image

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.

MarkBloodworth

Mark is an Architect Evangelist at Microsoft UK

He blogs at: https://remark.wordpress.com/

He Twitters at: https://twitter.com/blooders