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.
Let’s name it “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:
-
-
-
- [<EntryPoint>]
- let main argv =
- printfn"%A"argv
- 0
Let’s change it to write something on console.
- letSayHello() = printfn("Hello World!!")
-
- [<EntryPoint>]
- let main argv =
- SayHello()
- System.Console.ReadKey() |> ignore
- 0
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.
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”
This will interpret the code in the interactive code and you’ll see the output like below:
- >
-
- valSayHello : unit -> unit
- val main : argv:string [] ->int
-
- >
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:
- >SayHello();;
- Hello World!!
- 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:
- let Greet To =
- ifSystem.String.IsNullOrWhiteSpace(To) then
- "whoever you are"
- else
- To
-
- letSayHello() To =
- 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.
- if System.String.IsNullOrWhiteSpace(To) then
- printfn("inside the if..")
- "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,
- printfn("inside the if..")
- "whoever you are"
Now let’s run it interactive window. Select the these methods and right click in editor to choose “
Execute in Interactive”.
- val Greet : To:string -> string
- valSayHello : unit ->To:string -> unit
- val main : argv:string [] ->int
Now let’s invoke the method with argument value:
- >SayHello() "Amit";;
- Hi, Amit
- val it : unit = ()
And with default or empty value:
- >SayHello() "";;
- Hi, whoever you are
- 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.
- letrec fib n =
- if n < 2 then
- n
- else
- fib (n - 1) + fib (n-2)
-
- let find() n =
- printfn"%d" (fib n)
Now let’s run it:
- val fib : n:int ->int
- val find : unit -> n:int -> unit
- >find() 10;;
- 55
What we learned in this post:
- Writing a basic function and running in console application.
- Running program in Interactive window.
- Using Managed library method in F# code.
- Using If..else.
- Methods with arguments.
- A basic recursion program.
To learn more about F# keywords, Symbols and literals follow below links:
Read more articles on F#: