Compiling the first F# program using interactive mode

Posted by in F# category on for Beginner level | Views : 10307 red flag

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.
Title:         Compiling the first F# program using interactive mode
Author:      Shivprasad Koirala 
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  which is my own private site. I will surely improve on my writing.

Download and Installation of F#

Please note this tutorial has used F# compiler version you can download the same from 

You can download latest F# compiler from  . 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.

Type of Execution mode in F#

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-\bidn”).
• Other is interactive mode (using fsi.exe which is again found in the same directory “C:\Program Files\FSharp-\bin”).

Writing the first program

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", the declaration syntax

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

  1. Declares a method by name “MyMethodIncrement” with a logic which increments the value by one.
  2. This is the beauty of F# it has identified by the arithmetic calculation that the method will 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.

Page copy protected against web site content infringement by Copyscape

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)