Examining the F# Programming Language

In February 2017, Microsoft released a statement on a corporate blog about its .NET language strategy.

As expected, the posting covered C# and VB.NET in great detail—and it also detailed F#, which Microsoft claimed is used by “tens of thousands of people” and shows “great actual and potential growth.” (Compare that to the millions who use C#, or the hundreds of thousands for VB.NET.) Microsoft believes that F# will power a growing number of web and cloud services, tools and utilities, analytic workloads, and data manipulation (i.e., as a rival to Go or possibly Rust).

“On top of the strong functional legacy from the ML family of languages and the deep integration with the .NET platform, F# has some truly groundbreaking language features,” the blog added. “Type providers, active patterns, and computation expressions all offer astounding expressiveness to those who are willing to take the jump and learn the language. What F# needs more than anything is a focus on removing hurdles to adoption and productivity at all levels.”

F#, which originated at Microsoft Research in 2005, is now open-source and runs on Linux, Mac, Windows, Android and iOS. According to the 2016 fsharpworks survey, Windows (i.e. .NET) is still by far the most popular platform for F#.

What is F#?

F# is a complete re-implementation of the OCaml language for .NET by Microsoft Research, and heavily influenced by C#, Haskell, Python, Scala and Erlang.

If you think of F# as just a functional programming language, you’re missing out on its imperative and object-oriented programming facilities. Here’s an example from the F# snippets website:

let rec fib = seq {

yield! [0; 1]

yield! fib

|> Seq.pairwise

|> Seq.map (fun (prev, next) -> prev + next)

}

(No prizes for guessing that it’s a recursive function to calculate values in the Fibonacci sequence.)

Why F#?

What does F# offer that’s superior to C# or other, similar languages? For starters, C# is an OO language but F# is primarily a functional programming (fp) language. (If you are unfamiliar with functional programming, this 22-page “Why Functional Programming Matters” PDF is a good starting point.)

Functions are first-class citizens, so they can be passed around as parameters or stored in data structures. For instance, the tuple mixedTuple below holds an int, float, string, and function. Of course, this isn’t unique to F#. For example, Python also has first-class functions.

Let num = 12

let squareIt2 n = n * n

let mixedTuple = ( num, "two", 3.3, squareIt )

Conciseness is also a major factor, as F# has no curly braces { or semi-colons, so programs are generally shorter than equivalents in C#. Here is a very short Quicksort function:

let rec quicksort = function

| [] -> []

| first::rest ->

let smaller,larger = List.partition ((>=) first) rest

List.concat [quicksort smaller; [first]; quicksort larger]

Here’s an excellent example comparing C# and F# code in processing an abstract syntax tree. The F# code is one-third of the length of the C#.

F# has several features that help with program correctness. For example, all values are immutable by default and explicitly require the mutable keyword if not. The treatment of null is also helpful. You can use null with .NET types for compatibility but not F# types. F# instead provides the option type for handling data that might be missing.

One feature I’ve not seen in other programming languages is units of measure. This is an approach that prevents the type of errors that destroy spacecraft.

Floating point, signed integer and decimal types support dimensioned quantities (e.g., meters, pounds, etc.) In the example below, inch is defined as a unit of measure. The idea is to minimize errors because the variable has meaning; it’s not just a number. The function convertInchtoFeet does the conversion but will only work with inch variables.

[<Measure>] type inch

[<measure>] type foot

Let convertInchtoFeet ( length : float<inch> ) = length /12.0<inch> * 1.0<foot> .

Its foundation in the .NET framework provides F# with as much functionality as C#. It also includes C# features such as structs, extension methods, and events/delegates. Concurrency is built in, not just with asynchronous programming but using a message-passing approach (via agents that are lighter than threads and based on the same actor model used in Scala).

There are far fewer loops needed in F#. Many comparisons can be done with match keyword, which is a much more powerful version of switch.

F# for GPU Computing

You can run F# on the GPU, specifically using the NVidia CUDA model. It’s not the easiest thing to do and usually involves C-like programming languages.

Specifically, it is possible to write code in F# (or any .NET language including C#) using the cross-platform Alea GPU V3. It compiles directly to GPU machine code. There are three other ways listed which work for OpenCL, the rival to CUDA, using open-source tools.

Conclusions

If your background is C++/C# or Java and you want to give functional programming a try (away from LINQ, etc.), F# is probably easier to learn than, say, Haskell. Some developers even use F# alongside C#.

If you have the free version of Visual Studio 2015, you can install Visual F# 4.0 when you try to create or open an F# project. It’s a 116 MB download.

Post a Comment

Your email address will not be published.