Learning F# pt.1

There’s a lot of noise around functional programming in my bubble recently, so I’ve decided to give it a try. This time not only by using functional properties of C# or JavaScript but by playing with F#.

Why F#? Because it has # in its name, so one can assume that it has similar way of building solutions as C#, the language I’m most familiar with.

And that is true. F# is preinstalled (or easy to install at least) with Visual Studio so you can quickly jump to coding.

I’ve followed these tutorials and I guess I can recommend them so far. Don’t fear the This post is over 3 years old note at the top of the site – everything works as expected.

My goal was to write some code, get to know the syntax and check how to write tests for F# code and that’s what I’ve accomplished. The tutorials suggest coding and testing in F# Interactive Window which is fine for a while. Later, when one wants to write multiline functions, it turns into fighting with the terminal to insert new lines without executing code. Reportedly it happens only after writing ;; and hitting enter, but debugging terminal was not something I wanted to focus on.

That said I’ve created a simple solution with one file and been writing code in Program.fs file what enabled me to run the program in the console window.

Having overcome a few problems like F# errors not displaying in the Error list or syntax colouring turning off, I’ve went through the whole 60 seconds tutorial – such an accomplishment!

Of course, the goal was not just to skim through the page but to try each mentioned language feature. That means either just rewriting the code to get familiar but also modify the examples or extend them and write something I knew how to code in other language.

One slightly confusing thing I’ve found is that built-in types like List can sometimes be referenced with lower case:

But I guess it’s rarely allowed:

And finally: tests are supported, there’s even an xUnit package available.

Nice thing: test names can be just “strings” no need to bother with snake case. It is possible to name methods like this not only in tests but I wouldn’t recommend it due to obvious reasons.

One of the most interesting features of F# is code correctness. Not even newest C# version (11, supported on .NET 7) will warn you against working on empty array (enabling nullable will warn you against nulls though). F# does as shown below:

In the next steps I plan to build something more sophisticated and check how much more can F# keep my code correct and error proof just by using the language features.

Leave a comment

Your email address will not be published. Required fields are marked *