Click here to Skip to main content
Click here to Skip to main content

Compiling the first F# program using interactive mode

, 18 Oct 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Compiling the first F# program using interactive mode
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

Compiling the first F# program using interactive mode


 

 

Introduction
 

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.
 

Download and Installation of F#
 

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.
 

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

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.
 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shivprasad koirala
Architect http://www.questpond.com
India India

I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small
E-learning company in India. We are very much active in making training videos ,
writing books and corporate trainings. Do visit my site for 
.NET, C# , design pattern , WCF , Silverlight
, LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server  training 
and Interview questions and answers


Comments and Discussions

 
GeneralMy vote of 1 PinmvpDave Kreskowiak25-Feb-10 4:43 
GeneralMy vote of 1 PinmemberRichard Minerich14-Dec-09 5:02 
GeneralF# PinmemberJeff.Crawford19-Feb-09 2:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 18 Oct 2008
Article Copyright 2008 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid