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
So I saw in some places that functional programming may affect the way I think! And just to recap I am a C# developer.
Starting
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.
1 2 3 4 5 6 7 8 |
open System [<EntryPoint>] let main argv = printfn "Hello World from F#!" 0 |
After running the output is excepted or known. And adding the following line will hold the console window for user input
1 2 3 |
Console.ReadKey() |> ignore |
So writing the following code will read a name and show at the console. Start up thing
1 2 3 4 5 6 7 8 9 10 |
open System [<EntryPoint>] let main argv = printfn "Hello what is your name" let name = Console.ReadLine() printfn "You said your name is %s" name Console.ReadLine() |> ignore 0 |
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.
1 2 3 4 5 6 |
let mutable myValue = 100 printfn "Value of myValue is %i" myValue myValue <- 200 printfn "Value of myValue is %i" myValue |
Output will show the previous and post value. And if declared a value by reference then following syntax are in use.
1 2 3 4 5 |
let myRefValue = ref 10 myRefValue := 20 printfn "Value of myRefValue is %i" ! myRefValue |
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#
1 2 3 4 |
let myFunction(x: int, y:int): int = x + y printfn "The result of myFunction is %i" (myFunction(5,5)) |
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
1 2 3 4 5 6 |
let rec DoFactorial(x:int) : int = if x < 1 then 1 else x * DoFactorial(x-1) printfn "Result after calling DoFactorial %i" (DoFactorial 10) |
And the output is
1 2 3 |
Result after calling DoFactorial 3628800 |
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 .
1 2 3 4 5 |
let randomList = [1;2;3;4;5] let newRandomList = List.map (fun x -> x + 10) randomList printfn "The new random list is %A" newRandomList |
The output is
1 2 3 |
The new random list is [11; 12; 13; 14; 15] |
Pipeline function for nest function call
Following code block is to show newly generated list using pipe and filtering by condition.
1 2 3 4 5 6 |
[1;2;3;4;5] |> List.filter (fun x -> x % 2 = 0) |> List.map (fun x -> x * 10) |> printfn "My wanted numbers %A" |
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
1 2 3 |
My wanted numbers [20; 40] |
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.
1 2 3 4 5 6 7 8 |
let multiply x = x * x let add x = x + 1 let addThenMulti = multiply << add let multiThenAdd = multiply >> add printfn "Calling addThenMulti %i" (addThenMulti 5) printfn "Calling multiThenadd %i" (multiThenAdd 5) |
Here is the following output
1 2 3 4 |
Calling addThenMulti 36 Calling multiThenAdd 26 |
Loops
Following is the syntax of a while loop
1 2 3 4 5 6 7 |
let number1 = 1 let mutable number2 = 10 while not (number1.Equals(number2)) do printfn " Loop Executing" number2 <- number2 - 1 |
It shows the following output 9 times as excepted
1 2 3 |
Loop Executing |
Following syntax is for the for loop and “downto” has been used for decremental value and for incremental it will be “to”
1 2 3 4 |
for i = 10 downto 1 do printfn " For Loop Executing" |
Loop & List
1 2 3 |
[1..5] |> List.iter (printfn "Piped Value %i") |
Another
1 2 3 4 |
let sum = List.reduce(+) [1..5] printfn "Reduced Value %i" sum |
There are lots of syntax to be used for list and to iterate through.
Enum
We can declare a enum in the following way
1 2 3 4 5 6 |
type whether = | hot = 0 | cold = 1 | normal = 2 |
and that can be used like below
1 2 3 4 5 6 7 |
let todayWhether = whether.cold match todayWhether with | cold -> printfn("Whether is Cold") | hot -> printfn("Whether is Hot") | normal -> printfn("Whether is Normal") |
Conclusion
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.