United Kingdom: +44 (0)208 088 8978

Seven reasons to use F#

We're hiring Software Developers

Click here to find out more

Here at Compositional IT, we find that F# is a great general-purpose language for many kinds of applications. Here are seven reasons to use F#.

1. Correct code

In F#, several design decisions have been made to prefer safety and correctness, over short-term convenience. Here are just two examples:

  • Optional Values: The compiler does not allow null values to be placed into any type and therefore null reference exceptions are avoided. Many core library functions return optional values as opposed to throwing runtime exceptions, forcing the programmer to handle the null case correctly.
  • Exhaustive pattern matching: The compiler provides warnings when pattern matches don't handle all the available cases, rather than relying on the programmer to manually consider all these cases.

2. Concise and readable code

  • Clean and light-weight syntax: Indentation is used to define scope rather than braces.
  • Extensive type inference: The compiler can work out the type of most values so they don't need to be written out in full. Even function parameters!
  • Pattern matching provides easy to follow de-structuring of values and branching based on their shape or values.
  • Expression oriented: Almost everything is an expression that can be nested inside other expressions, e.g. if/then/else

Here's a function that demonstrates these features:

let describeNumber n =
    match n with
    | Some 0 -> "zero"
    | Some 1 -> "one"
    | Some n when n < 0 -> "a negative number"
    | Some n -> "a positive number"
    | None -> "no number 🤔"

3. Effective domain modelling

By separating type definitions and behaviour, it's possible to model an application's domain clearly concisely in one place. This allows a quick overview of the domain that can even be understandable for non-technical stakeholders.

Discriminated unions provide a way of specifying "or" types. In this example, a customer has a preferred contact method, which is either an email address or a phone number, and is optional:

type ContactMethod =
    | Email of EmailAddress
    | Phone of PhoneNumber

type Customer =
    { Name : string
      PreferredContactMethod : ContactMethod option }

4. Support for functional programming

F# has simple programming model where individual functions are isolated and easy to understand. Idiomatic F# is about functions and immutable values, not about classes, inheritance and mutable properties.

  • Functions are first class values: They can be easily defined as values, passed around and partially applied.
  • Powerful immutable data structures such as records, lists, sets and maps support and encourage the functional style.
  • Advanced collection functions: In addition to the basic functional programming functions like map, filter and fold, we have choose, collect, groupBy, tryPick, scan and many more. These cover many use cases that allow concise and readable processing of data.

5. Interactive development with FSI

Using F# script files and F# interactive (FSI) you can quickly try out code and inspect the results. This gives a very fast feedback loop for trying out new code or understanding existing code. See our other blog posts on scripting in F# for more details.

6. Many target platforms

  • Windows, Linux and Mac: As a .NET language, F# can run on .NET Core.
  • Web browsers: Write F# code for your web front-end using Fable, which compiles F# to JavaScript.
  • Mobile apps: Write F# apps for iOS and Android using either React Native with Fable, or Xamarin and Fabulous.

7. The eco-systems

  • Host platform integration: If running on .NET Core, then you have direct access to the Base Class Library and frameworks like ASP.NET Core. Using Fable in the browser, there's convenient and idiomatic access to browser features.
  • Good interoperation with existing libraries: F# supports direct and easy use of any existing libraries on the host platform: e.g. Serilog for .NET and D3 for JavaScript.
  • Libraries designed for F#: There are many libraries designed specifically for use with F# that can be used in idiomatic F# style. Examples include FSharp.Data for data access and Elmish for building UIs.

How to start using F# now

Need any additional support to get you and your team up to speed? We're here to help!