Functional programming is often seen as the scary math heavy alternative to the popular imperative style of programming. In truth functional programming is neither scary or an alternative! The programming languages you know and love contain many functional parts and you've written functional code without realizing.
F# has been guiding the development of C# behind the scenes for many years and will no doubt continue to influence design choices as long as both languages exist. There's no better time to learn about functional programming than the present and thanks to the design of dotnet it's as easy to start with as ever.
As a first class dotnet language any installation of the SDK already gives you the tools needed to start writing your first F# program. If you're already convinced head on over to Microsoft Learn to get set up and The F# Foundation for a comprehensive set of guides.
For those who want to learn more before diving in we'll look over what functional programming means for practical development and how learning F# can improve your existing skills.
[<EntryPoint>]
let main argv =
printfn "Hello World!"
0
A familiar example to any programmer: write text to the output. The JS developers might see a familiar let
statement; those with a C background will almost certainly look at printfn
with some familiarity. It might help
to compare our example to the same in C# and find some similarity before looking at the differences.
static public int Main (string[] argv)
{
Console.WriteLine("Hello World!");
return 0;
}
Both examples show a function being created called main
which takes in some list of arguments assigned to
argv
. Inside both are invoking some other function to print a string containing our Hello World! after which
both are doing something with the number 0.
Looking at them together it feels like there are more differences than similarities. F# does away with our famous braces to define blocks and opts for whitespace. There may be cries against significant whitespace but in all my years of writing code I've never seen C# that wasn't indented just the same!
A similar story is told about the dreaded semi-colon. Yes you can write multiple statements on one line by using it but I challenge you to find any project that has more statements on one line vs statements that take up multiple lines!
Once through the initial shock you might start noticing something about the F# example, something that seems fundamental but is stragely absent, where are the types! Isn't this language the same as C# with strong typing and compile time checking? Surely F# can't change those fundamentals as well... you are correct: F# is a strongly typed language with compile time checking just like C#.
Looking back at our example the main
function in both cases has the same signature and returns an integer. This
is one of the first major differences between the two languages. By placing a zero on the last line of our function it
becomes the return value and since that value is an integer the function must return an integer.
There are many more differences to cover like why is the last value returned but hopefully this little example can show that F# is not an obtuse academic only language. If this has sparked your interest then get in touch and let your curiosity guide you!