Compiling the first F# program using interactive mode

In this tutorial we will compile the first F# program using the interactive mode. There are lot of talks going on regarding F# , just thought how about compiling the first program using interactive F# mode.

If you think you like the article or you think I should improve drop me a message on http://www.questpond.com/ which is my own private site. I will surely improve on my writing.

You can download latest F# compiler from http://research.microsoft.com/fsharp/release.aspx . You need to first install .NET 2.0 or greater and then F#. Once you are done with the installation you should find F# in your program menu as shown in figure "F# Interactive Console".

. Full compilation to .NET code (using fsc.exe which is found in "C:\Program Files\FSharp-1.9.2.9\bidn").. Other is interactive mode (using fsi.exe which is again found in the same directory "C:\Program Files\FSharp-1.9.2.9\bin").

So let's try to write our first program using the F# interactive. For some time we will be using the F# interactive so that we can get hold of the concepts. So click on Programs ÃƒÂ Microsoft Research F# ÃƒÂ F # interactive console. Once done you will see a screen as shown in figure "F# Interactive Console". The arrow in the figure shows the place where we will enter F# commands. All F# commands end with a ";;" (i.e. double semi-colon).

Figure: - F# Interactive console.

So let's type our first program. Below figure shows a simple program which sets a string value and displays the same on the screen. We have numbered the commands let's understand every command number by number.

Figure: - Your first program

1 and 2 - "let" keyword binds a identifier (i.e. "x" in this case) with value "Shiv". After you end the command with ";;" and press enter you will see "Val x : string". This is a information given by the F# compiler saying that I have declared your variable "x" as a string. In short the compiler evaluates the type. If you note in C# and VB.NET you need to define the variable data type. Below figure "Comparison" shows how the "let" keyword maps to C#. "let" is a declaration keyword in F#.

Figure: - Comparison

3 and 4 - In this command we have asked the F# compiler about the variable "x". In line 4 we can see the answer of the compiler saying "Val it: string = "Shiv" which means "x" is a string data type with value "Shiv".

5 - Here we use the System.Console.WriteLine to display the data of the variable. One of the things to be noted is that "System" is .NET namespace. In one line we can say F# can use .NET functionalities in a seamless manner.

"Let" keyword in F# allows you to declare variables and methods. Below figure "Declaring variable" shows how to declare a variable. One of the important points to benoted is that we do not need to define a data type. F# depending on the value figures outwhether the declaration is a string or an integer. For instance you can see when we gavevalue "2" F# identified the data type integer while when we a gave a string value "shiv" itidentified the data type as string.

Figure: - Declaring Variable

Note: - The syntaxes are bit different from what you do in normal programming. So one of the important things in learning F# is thinking that it's a complete new language. If you try linking the syntax styles with old C# and VB.NET we are sure you will be lost.So clean your mind and think fresh.

Methods are also declared using the "let" keyword. Below figure "Comparing F# and C#method declaration" shows how F# methods are declared as compared to C#. For instance we have declared method called as "MyMethodIncrement" which takes anumeric value and increments it by one. The method body follows after the method namein F#. There are no curly brackets like C#. What ever variables we use in the F# methodbecomes the input parameters for the method.

Figure: - Comparing F# and C# method declaration

Now let's run through the method and see how it works. Figure "Declaring method" shows we have declared the method and called it. Let understand the figure in the numbered fashion.1:- Declares a method by name "MyMethodIncrement" with a logic which incrementsthe value by one.2:- This is the beauty of F# it has identified by the arithmetic calculation that the methodwill return a integer,3 and 4:- Here we call our method once by giving value 2 it increments and displays 3.Then we again call by value 3 it increments and displays 4.

Figure: - Declaring Method

Note :- If you see the beauty of the above method declaration is that F# identified that this function will return a integer by seeing the arithmetic calculations..while in C# we need to explicitly define saying what our function will return.