In my previous post Better Functional Programming Support Is Coming In C# 7 I argued that by the time you get a chance to write in C# 7 you should already be familiar with Functional Programming paradigm. But since you cannot use C# 7 at the time of writing – you should definitely consider F# for learning the concepts of Functional Programming. I hope we’ll be able to write elegant declarative-style code in C# as we do in F#.
It is hard to convince somebody to spend their time on something new without showing immediate benefits. But the benefits usually come in the form of idiomatic language features which can be overwhelming at first. I hope this post will show you immediate benefits of Functional Programming by looking at real code example and understanding the features one-by-one. I do not intend to present all of the features of F# – just enough to get you interested in Functional Programming.
let rec quicksort = function |  ->  | x :: xs -> let smaller = List.filter ((>) x) xs let larger = List.filter ((<=) x) xs quicksort smaller @ [x] @ quicksort larger
I was just amazed how much functionality was packed into these 6 lines of code! If you are new to Functional Programming and F# in particular, it could be hard to appreciate this code, so let’s review feature-by-feature what makes this piece of code so elegant and powerful.
If you are curious to see how quicksort implementation looks in C# – scroll to the bottom of the post.