Essential Concepts And Tools For Learning F#

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:

  • F# programs follow a Top-Down Structure.

  • In F# you can write Object Oriented Programs (OOP) or F# program (FP) or a combination of OOP and FP.

These are the three ways of creating a F# Program Structure.

  1. 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
  2. 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()

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

  • Single line comments- Single line comments are represented by double slash symbol(//). Everything which is follows by // considered as a comment.

//This is a single line comment

  • Multiline comments- Multiline comments start with (* and end with a*) symbol.

(* This is Showing multiline comments*)
(* This is the way to show multiline comments in F#*)

  • Document comments- Document (Doc) comments is represented by triple slash (///) symbol. These comments are generally placed before the definitions of Classes, Data Types and Functions etc.

///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:

  • abstract

  • and

  • as

  • assert

  • base

  • begin

  • class

  • default

  • delegate

  • do

  • done

  • downcast

  • downto

  • elif

  • else

  • end

  • exception

  • extern

  • false

  • finally

  • for

  • fun

  • function

  • if

  • in

  • inherit

  • inline

  • interface

  • internal

  • lazy

  • let

  • match

  • member

  • module

  • mutable

  • namespace

  • new

  • not

  • null

  • of

  • open

  • or

  • override

  • private

  • public

  • rec

  • return

  • static

  • struct

  • than

  • to

  • try

  • type

  • upcast

  • use

  • val

  • void

  • when

  • while

  • with

  • yield

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.

fact3.gif

// 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

fact2.gif

Summary

In this article I covered the basic concepts and tools essential for F#.


Similar Articles