X
Business

F# and functional computing languages: Out of the lab and into your business

Functional languages are here to stay. So are you ready to completely change the way you code?
Written by Simon Bisson, Contributor
programming
As the IT world changes, so too do the programming languages we use. Image: Shutterstock

The sands of the IT world are shifting, as the cloud and mobile move from being secondary to the old n-tier+web model that's dominated for much of the last decade. That sea change is shifting the languages and tools we use, away from the C family of languages (such as C++, C#, and Java) to one that descends from more formal languages such as Lisp.

Functional programming languages were, until recently, languages for computer science. Based on a pure mathematical approach to programming, they were the basis of much modern computer science research, with languages such as ML, Haskell, and oCaml. One of the first commercialised functional programming languages was Erlang, developed to reduce the risk of errors in telephone networks. But now they're shifting to the desktop — and, more importantly — to mobile devices, as well as programming cloud hosted PaaS platforms. 

It's important to understand the difference between traditional imperative programming techniques and functional programming. Functional programming is stateless, with mathematical functions at the heart of its operation. A function will always give the same result for the same inputs; it's never dependent on results from another function. That makes it easier to understand how a program will behave, something that's important when you're working with a range of external inputs — especially if you're working with streamed or asynchronous cloud data.

That does mean that programming with functional languages isn't as easy as it is with traditional programming tools, as you'll need to understand some very different concepts. A more mathematical approach to programming means you'll need to rethink some common programming structures and methods, especially where you need to manage state.

Apple's Swift, with its roots in Objective C's Lisp heritage, is helping take functional programming out of the classroom and into the real world. While it's designed to be familiar to C programmers, its programming structures owe much to languages such as OCaml.

It's interesting to see Apple focusing on a functional language as its new programming platform for mobile devices, and even more interesting to see it being wrapped in a way that makes it easier for C-family developers to pick up. Swift isn't a pure functional language, it's more like Clojure, with structures that can handle state directly. However, it does give you many of the features of a functional language, and as such will be easy to learn.

Microsoft too is moving much of its language focus to functional programming, as it builds on the work of the MSR programming group. Part of that work has led to the release — and now the open sourcing — of F#, another language that owes much to ML and OCaml. It's also added functional programming features to its C# language.

As F# is a .Net language, it's not surprising that Xamarin has recently added F# support to its tools in response to strong demand from the financial services industry. But F#, like Swift, is ideal for building the smart endpoints that we need for effective cloud apps. The cloud is a powerful tool for processing large amounts of information, but it needs tools to extract and manage that information effectively — tools that need to work effectively with asynchronous data sources, and that can be trusted to give the correct responses.

There's a lot that's interesting in F#. For one thing, it uses type inference to handle strong-typing when your program is compiled. You don't need to declare a variable type; the first time it's used, it'll be set appropriately — though you can set types for your own reference, making your code easier to read.

One area where languages like F# simplify working with data sent back from cloud services is in how they work with tuples, records and lists. If you're always getting back the same number of results in a call, then you can use a tuple to store the data. Alternatively, if (for example) you're working with a name/value NoSQL source, you can use named records to handle writing and reading data from the source in an easy to understand way.

Where F# really works well for cloud endpoints, especially for apps that are consuming multiple APIs at once, is with its support for asynchronous and parallel programming structures. Asynchronous workflows are easy to construct, giving you the tools to build callbacks that work with data that can be delivered at any time after a request. There's also the option to run several asynchronous blocks of code in parallel (code that can be run on GPUs as well as using the standard Windows threading models).

F# has become a first class language in Visual Studio, and there's support for working with common Windows toolkits and APIs, including .NET classes. This means that you can write Windows Forms aps in F#, as well as working with Microsoft's LINQ in-line data tools. To get a feel for working with F# and functional programming, Microsoft Research runs an online F# interactive tutorial, where you can write code without leaving your browser.

Functional programming is a powerful technology, and one that's finally making its way from the research lab into your favourite IDE. You're probably using apps written using these languages already, as they're running the back ends of many major cloud services, as well as running complex financial systems — and social games. But it's in our pockets that it's going to make a difference, giving developers tools to write reliable, and predictable, code that copes with the complexities of the modern mobile network.

Further reading

Editorial standards