Essential concepts to understand F#
The F# interactive language can either be used from Visual Studio or by running the fsi.exe from the F# installation directory. In F# we also use F# lightweight syntax which makes the code Whitespace Sensitive. If you want the lightweight syntax to be enabled, you have to enter the following command in FSI:
#light;;
In F# the double semicolon is used to terminate the FSI input and sends the text to the compiler.
Program Structure of F#
The F# program structure can be created using a standard text editor or IDE like Visual Studio 2010 or Visual Studio 2008. F# has an advantage of intellisense and highlighting syntax. F# contains a well defined set of rules, syntax and keywords. When you first create a F# program you should know how the F# program structure is organized. While creating a F# program you have to remember some points:
These are the three ways of creating a F# Program Structure.
- OOP structure-
namespace FSharp.tutorial //namespace
open System //open declaration
Type Person() = // declare types/members/ variables
let mutable num = 0
let mutable fname = String.Empty
member a.Num
with get ()= num
and set(b) = num <- b
member a.Fname
with get() = fname
and set (b) = fname<- b
-
FP Structure-
module PersonModule // module
Type PersonRecord = { //open declaration
Num : int
Fname : string //declare records/ let variable/ FP code
}
let getPerson() =
{ Num = 0; Fname= Shalini; }
let per = getPerson()
-
When we combine the two OOP and FP structures together the new structure will be:
namespace FSharp.tutorial //namespace
open System // open declarations
/// Person Record
Type PersonRecord = { //record declaration
Num : int
Fname : string
}
Type PersonDetails() = // class declaration
let getPerson() = // let binding to get person record
{ Num = 0; Fname= Shalini; }
let per = getPerson() // let binding variable per
///member declaration for person
member a.person //member variable person returning the per variable
with get() = per
You will follow some rules. When you combine the F# and OOP structure:
-
F# let binding should be declare in top of Type structure
-
If you declared any member in the bottom it can be referred to even from the top of the program, because OOP code doesn't follow a Top-Down programming structure.
WhiteSpace Matters
WhiteSpaces are a combination of spaces and newline characters, when you write first F# program and use #light directive, whitespaces become significant for the structure and execution of the program. C# uses braces for grouping of statements where as F# uses Whitespace indentation for the same. Regarding Whitespace you should notice two basic points:
-
The number of Whitespaces doesn"t matter; you can use more than one space.
-
If you are using 4 spaces, then that is equivalent to a tab key and the Visual Studio IDE automatically convert tabs into spaces.
Comments
F# provides three types of comments named single line comments, multiline comments and document comments.
//This is a single line comment
(* This is Showing multiline comments*)
(* This is the way to show multiline comments in F#*)
///This is showing document comment
Keywords
Keywords are those predefined words which have special meaning to the Compiler. F# has a set of reserved Keywords, which are as follows:
Let's take an example of F# to calculate the factorial of a given number.
Step 1: Open Visual Studio and select F# Application Project.
Step 2: Write the following code in the F# editor.
// Learn more about F# at http://fsharp.net
open System
let rec facto x =
if x < 1 then 1
else x * facto (x - 1)
(* // can also be written using pattern matching syntax:
let rec fact = function
| n when n < 1 -> 1
| n -> n * fact (n - 1) *)
Console.WriteLine(facto 5)
Step 3: Press CTRL+F5 to run the program.
Output
Summary
In this article I covered the basic concepts and tools essential for F#.