Title: Compiling the first F# program using interactive mode Author: Shivprasad Koirala Email: shiv_koirala@yahoo.com Language: ASP.NET Level: Beginner Description: F# Interview Questions

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.



Please note this tutorial has used F# compiler version 1.9.2.9 you can download the same from http://research.microsoft.com/research/downloads/details/52ba6040-6998-4de2-bfc6-f7a0f40c71e2/details.aspx



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”.







Figure: - F# Interactive console

That is all you need to learn F#...So let us start.



There are two types of execution mode in F#:-



• 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.



Figure: - F# and .NET



6 – We can see the F# compiler replying back with display “shiv”.



“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 be

noted is that we do not need to define a data type. F# depending on the value figures out

whether the declaration is a string or an integer. For instance you can see when we gave

value “2” F# identified the data type integer while when we a gave a string value “shiv” it

identified 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 a

numeric value and increments it by one. The method body follows after the method name

in F#. There are no curly brackets like C#. What ever variables we use in the F# method

becomes 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.