F# vs C# - Sorting

How do the languages compare when sorting elements?

First look

The first look on the sorting tutorial page is the quicksort. It took me a bit longer to understand this example if I’m honest but after running it here I understood it more and more. I would still find it much easier to write in C# but I appreciate the concise nature of F#.

Diving deeper

The post explains the benefits of F# again such as the lack of noise from the method signatures and type declarations within C# and the correctness and self documenting nature of F#. The “Postscript” of this post was the “quicksort2” which shows how this problem would actually be solved with F# not in an easy to read demonstration way;

let rec quicksort2 = function
   | [] -> []                         
   | first::rest -> 
        let smaller,larger = List.partition ((>=) first) rest 
        List.concat [quicksort2 smaller; [first]; quicksort2 larger]
        
// test code  
printfn "%A" (quicksort2 [1;5;23;18;9;1;3])

I think it still still be a while before I can jump in and write F# code like this. It’s very impressive

Written on September 10, 2018