Hands On F#

So let’s jump to the point straight way.

What is F#?

F# is Microsoft’s functional programming language. It provides way to develop predictive code which is often shorter. And this is a Major Benefit for functional language. We can say these types of language applies function to data rather than focus on state changes like OOP languages. These are also great to execute multiple process at the same time.

Why learning a new language like F#

A language that does not affect the way you think about programming is not worth knowing

Alen Perlis

So I saw in some places that functional programming may affect the way I think! And  just to recap I am a C# developer.


I am using Visual Studio 2017 Version 15.8.9. The full code of this whole article is available to Github. Creating a new Console Project shows as usual File Structure and at the Program.fs (.fs is a F# extension file) shows the following lines.

After running the output is excepted or known. And adding the following line will hold the console window for user input

So writing the following code will read a name and show at the console. Start up thing

This is the first commit at Git. 

Let’s see the following example. If a variable’s value is changeable we need to define the value as “mutable”. and to change the assign value we change the value in following way.

Output will show the previous and post value. And if declared a value by reference then following syntax are in use.

This is the second commit at Git. 

Let’s start talking about the functions as we are discussing about functional language. Following is the syntax of how easy a function can be declared and can be used. Function can be written inside a function in F# and so in C#

And the value is 10 as excepted.

Recursive Function

Let us implement a recursive factorial function and the following is the complete code of the of the function and also shows the calling of a function and the output. At the same time we will also see the syntax of if & else statement

And the output is

Which is the factorial value of 10. Feels like mixture of Node, Python style syntax etc.

List, Lambda

Following syntax is to declare a list then at the next line we declare a new list from the first list. List.map has been used inside that a function has been declared using lambda expression and the previous list is passed as parameter. And %A is used to get the values of inside of that list .

The output is

Pipeline function for nest function call

Following code block is to show newly generated list using pipe and filtering by condition.

So, 1st line is the list then it piped through a filter and takes only even numbers then multiply those with 10 and then shows the following output

Using multi functions

1st line is a function which takes a value and square itself. 2nd line just add 1 with the input. Now 3rd and 4th function is a combination of first 2 functions and the function at line 3 first call add function then multiply and the function at 4th line first multiply then add.

Here is the following output


Following is the syntax of a while loop

It shows the following output 9 times as excepted

Following syntax is for the for loop and “downto” has been used for decremental  value and for incremental it will be “to”

Loop & List


There are lots of syntax to be used for list and to iterate through.


We can declare a enum in the following way

and that can be used like below


There are lots more stuff to learn the. F# documentation can be found on Microsoft. But after learning more on this i found out it is actually eliminating the need of writing more lines of code than C#. But end of the day I would say I am still very much comfortable with C# for the professional project. May be over the time C# will  adopt this code shorten syntax and then I will keep using the C#!!!

The codes available at Github.

Leave a Reply

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