Getting Started With F#

In a previous post we looked at the high level overview of Function programming language and introduction to F#. In this post we will start with basic things to learn Visual F#.

Tool – Visual Studio 2013.

We’ll start with a console application. Launch the visual studio and add a new F# console application.

console application

Let’s name it “FSharp.Starters.Console”.

FSharp.Starters.Console

Open the Program.fs file in editor and let’s write something in it. You’ll see an entry point method something like below:

  1. // Learn more about F# at http://fsharp.net  
  2. // See the 'F# Tutorial' project for more help.  
  3.   
  4. [<EntryPoint>]  
  5. let main argv =   
  6. printfn"%A"argv  
  7. // return an integer exit code  
Let’s change it to write something on console.
  1. letSayHello() = printfn("Hello World!!")  
  2.   
  3. [<EntryPoint>]  
  4. let main argv =   
  5. SayHello()  
  6. System.Console.ReadKey() |> ignore  
  7. // return an integer exit code  
run

There are two ways you can run and test this F# code. Either via console application or using F# interactive windows. You can open the F# interactive windows in Visual Studio from menu View, Other Windows, then clicking F# Interactive.

OtherWindows

Now you can go to Program.fs and select all code and press Alt + Enter to run the code in interactive window. If this doesn’t work then you can select all and right click in code editor and select “Execute in Interactive

Code

This will interpret the code in the interactive code and you’ll see the output like below:
  1. >  
  2.   
  3. valSayHello : unit -> unit  
  4. val main : argv:string [] ->int  
  5.   
  6. >  
Now the method ‘SayHello’ is ready to run. To invoke any method in Interactive we run it by typing method name and ending it with double semi colon e.g. “MethodName() ;;”. So let’s run the above code right away:
  1. >SayHello();;  
  2. Hello World!!  
  3. val it : unit = ()  
Congratulations! You now know how to run any Fsharp program/script. Let’s go ahead and put some arguments in the SayHello() method.

To declare method argument is simple, just write the next to the method name like below:
  1. let Greet To =   
  2. ifSystem.String.IsNullOrWhiteSpace(To) then  
  3. "whoever you are"  
  4. else  
  5. To  
  6.   
  7. letSayHello() To =   
  8. printfn"Hi, %s" (Greet To)  
Important Note: Next statement or method definition is identified by four spaces or single Tabs in visual studio. Because there are not block defining like curly braces in C#  it’s all defined via indentation.

E.g. Below statement would print “inside the if..” and return value “who ever you are”. The last statement in the same indentation is returned value.
  1. if System.String.IsNullOrWhiteSpace(To) then  
  2. printfn("inside the if..")  
  3. "whoever you are"  
Take below example – the if statement would only print “inside the if..” the return value is out of if block.

if System.String.IsNullOrWhiteSpace(To) then,
  1. printfn("inside the if..")  
  2. "whoever you are"  
Now let’s run it interactive window. Select the these methods and right click in editor to choose “Execute in Interactive”.
  1. val Greet : To:string -> string  
  2. valSayHello : unit ->To:string -> unit  
  3. val main : argv:string [] ->int  
Now let’s invoke the method with argument value:
  1. >SayHello() "Amit";;  
  2. Hi, Amit  
  3. val it : unit = ()  
And with default or empty value:
  1. >SayHello() "";;  
  2. Hi, whoever you are  
  3. val it : unit = ()  
Hope you’re getting more confident, now let’s do something more, like writing an algorithm to print nth Element of Fibonacci series. We’ll be using recursion to find the value. In F# the recursion is achieved via specifying a special keyword ‘rec’ with function. If you don’t specify the ‘rec’ word you’ll get the error while trying to achieve recursion.
  1. letrec fib n =   
  2. if n < 2 then  
  3. n  
  4. else  
  5. fib (n - 1) + fib (n-2)  
  6.   
  7. let find() n =   
  8. printfn"%d" (fib n)  
Now let’s run it:
  1. val fib : n:int ->int  
  2. val find : unit -> n:int -> unit  
  3. >find() 10;;  
  4. 55  
What we learned in this post: 
  1. Writing a basic function and running in console application.
  2. Running program in Interactive window.
  3. Using Managed library method in F# code.
  4. Using If..else.
  5. Methods with arguments.
  6. A basic recursion program.

To learn more about F# keywords, Symbols and literals follow below links:

Read more articles on F#: 

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now