The Trip to F#

As someone who has written Python for almost 25 years, spent years writing various Lisps and Smalltalk, and spent a few years in the Clojure community, it might be interesting to talk about what appeals to me with F#. Appeals enough to get me to restart my blog after an 11 year hiatus (2000-2009 being the original dates). There are a few things that appeal:

  • Practicality while still having an opinion, which seems to be derived from its OCaml roots.
  • .NET platform.

Let’s take each of those in turn. The first is that I’ve had an off-and-on love affair with functional programming over the years, and yet I’ve never committed totally to anything of the sort. It started with various Lisps in the late 1980s and 1990s, culminating with the ownership of a few different Lisp machines. There was that weird dalliance with Objective-C in the early NeXT era. In the 1990s, I wrote a lot of Perl, and then moved to Python around the time it hit version 1.21 and that was my main language for a very long time. I stuck around the Clojure world for a few years, and what appealed to me at first there is what appeals to me with F#: an appeal to functionalism with a practicality that acknowledges the rest of the world adopted OOP. This is not something I found in communities like Haskell, for example, where purity seemed to be its own reward; something interesting in academia, perhaps, but not when you’re trying to solve “real world” problems. Sadly, the Clojure community changed, and I’ll let other people write about that.

So that brings us to around the end of 2019 or early 2020; in this pandemic era, it’s difficult to tell time. I dabbled with Rust for a bit, but right now I’m just not that interested in systems-level programming. At work, I live in the world of Python, Linux, the JVM, Scala, and all of the associated detritus. I wanted something totally different, and historically, I’ve not really known the Microsoft world as well as I probably should. So, when a friend mentioned F# to me, I started digging into it, and liked what I saw. F# has OCaml roots, but, like Clojure on the JVM, benefits from being on top of a major platform with all of the associated libraries. Unlike Clojure, F# has a gigantic benefactor that is invested in it: Microsoft.

It’s also eminently readable, unlike many other languages (I’m looking at you, Scala). Peek this little code fragment, taken from Microsoft:

// 'name' is inferred to be a string based on usage.
let printMessage name =
    printfn "Hello there, %s!\n" name

// 'names' is inferred to be a sequence of strings.
let printNames names =
    |> Seq.iter printMessage

let names = [ "Ana"; "Felipe"; "Emillia" ]
printNames names

Uncluttered, and stripped to the minimal syntax. let is used for everything assignment related (basically), which reminds me of LISP-1 world. Functions are, obviously, first class things, and while not obvious from this snipped, they are auto-curried so partial application is natural. And, F# embraces pattern matching in the way you would expect for a functional language:

type Shape =
    | Square of side: double
    | Rectangle of width: double * length: double

let getArea shape =
    match shape with
    | Square side -> side * side
    | Rectangle (width, length) -> width * length

let square = Square 2.0
printfn "The area of the square is %f" (getArea square)

The ease with which new types are constructed and matched mean that doing a lot more nuanced domain-specific modeling is trivial and comes with minimal burden compared to some other language. Yes, I realize that this is also something Haskell, and many other functional languages can do.

It is interesting that, in 2020, .NET is the more friendly choice than the JVM in many ways. Microsoft has embraced developers historically better than anyone, but now they’ve also embraced the open-source community as well. Sun was developer-friendly, but Oracle is only friendly to lawyers, and then only their own.

So here we are, in 2020, with someone who has fastidiously avoided being on Windows and stayed out of the Microsoft orbit almost entirely digging into the core of the Microsoft universe. That should be fun.

  1. It’s been a long, strange trip from 1.2 to 3.9, but not as long as the trip from IPv4 to IPv6. That is a story for another time. ↩︎