6 reasons to love F#

Terje Tyldum

F# is a fun language and as a C# programmer in my day job this is my 6 favorite reasons to love F#

1. Need to test out code?

This is what the REPL (F# interactive window) is for. You have created a new function and want to test it out. No need to create a Console application or a new test method. Just copy the function into the REPL and call it. Perfect for exploratory coding.

2. Its functional first

Need a simple function? There is no need to create a class, just write your function and call it. In my opinion classes and objects are quite alien to non-programmers so I think functional languages actually are very suited as starter languages. Since F# is functional first you still can write object oriented if that fits your problem. Compared to C#, F# simply gives you more tools in your toolbox.

3. Pattern matching

let rec fib n = match n with | 1 | 2 -> 1 | n -> fib(n-1) + fib(n-2)

"fib" is a recursive function with a single parameter "n". If n is 1 or 2 return 1 else return the sum of the two previous numbers in the fibonacci sequence

Pattern matching is called switch statements on steroids and maybe since switch statements have gotten somewhat of a bad reputation in C# many C# developers may dismiss this feature too easily. The fact that many "newbie" developers love "switch" and "if" statements is because they are very easy to understand and makes for very readable code. Pattern matching fixes many of the problems with switch statements and adds a lot extras while maintaining the readable code. Read more about the features of pattern matching here.

4. Functional features often result in fewer bugs

F# like most functional languages have lots of features that makes it easy to write bug free code. No null references and immutability is a couple of examples here. Immutable collections will in some cases give you a little lower performance than mutable collections but with todays hardware and optimizations this is not a big issue anymore. Correctnes is almost always more important than performance anyways and again in F# you can have mutability if you need it.

With its strong type support F# is also perfect to create domain logic. It is very easy to create new types and by using these types you can make unsupported states in your program unrepresentable.
Scott Wlaschin has a complete series on this http://fsharpforfunandprofit.com/series/designing-with-types.html

5. Type Providers and Unit of Measure

Type providers is F#'s killer feature. It makes interacting with data in any format very easy. There are hundreds of providers and with a couple of lines of code you can interact with databases, rest services, excel sheets in a strongly typed fashion. Examples can be found here http://fsharp.github.io/FSharp.Data/index.html

Another feature that will help you create bugfree code is Unit of Measure. With Unit of Measure you can annotate numeric data with extra metadata that will help you avoid mixing up numbers of different types like inches and centimeters. http://fsharpforfunandprofit.com/posts/units-of-measure/.

6. Community

F# is fully open source and has a very strong, enthusiastic and friendly community. Its popularity is rising and I think it deserves more love from its original creator Microsoft. Still it is fully supported in Visual Studio and Xamarin Studio and its community will make it very hard to ignore in the years to come.

More resources

comments powered by Disqus