Introducing the F# Programming Language

F# (pronounced “F Sharp”) is a functional programming language built into the .NET—just like C#, J# and Visual Basic. Furthermore, F# can take advantage of the same core .NET libraries as the three mentioned languages. F# is a bit different from the three mentioned programming languages; in fact, F# has inherent technical strengths as a multi-paradigm programming language, uses type interference, supports LINQ, provides F# interactive visualization and includes all the important elements of functional programming.

F# was originally started at Microsoft Research, Cambridge in 2005. The Microsoft Research (MSR) team continues to improve F#, and continues its partnership with other teams across the company and with—external open source organizations, researchers, and companies. F# is currently shipped with Microsoft Visual Studio 2010 by default, however, is also included in Microsoft Visual Studio 2012.

Basics

Here are a few basic lines of code I will explain:

open System 

let msg = "Hello World" 

printfn "%s" msg 

Console.ReadKey() 

When compiled and executed in debug mode this code will print out ‘Hello World’ in a console window.

The open works like using in C#, we tell it what namespaces we want to open, in this example I used the standard System namespace. The letkeyword is used to bind expressions, or to define values or function values for one or more names. The printfn prints a formatted value to stdout, and adds a newline (‘\n’); The format styling looks pretty much like C‘s format styling, consisting of strings with % markers indicating format placeholders.

Declaring variables is very straight forward, F# does not need to have the data type predefined for it i.e. int i = 0; here we tell it that we want to declare an int. In F# you don’t have to do it this way, it detects this by value.

let i = "" // string 
let i = 'a' // char
let i = 0 // int 
let i = 0.0 // float
let i = true // bool
let i = "text"B // byte[]

Creating functions is very easy too, however, can be very painful and dirty first time, especially, if you like me are used to declaring functions like this:

int multibytwo(int x) { return x * 2; }

well this does not work in F# unfortunately, and it will seem very wired, but here’s how you write the same function in F#:

let multibytwo (x : int) = 2 * x

With just a few examples of F# code we see the difference between it and C# and many other programming language that share the .NET Ecosystem.

Math fun with F#

F# has been suggested to be used to solve math problems, or try out different math functions and equations. Here’s the basic code for multibytwo (x : int) = 2 * x :

open System; 

let multibytwo (x : int) = 2 * x
let v = multibytwo(500)

printfn "Equals %d" v

Console.ReadKey(); 

Here’s another interesting example, N^2:

let square x = x * x

let numbers = [0 .. 10]

let squares = List.map square numbers

printfn "N^2 = %A" squares

The second code example can be a bit complex to understand, the very first line is very obvious, so I will explain the second, let numbers = [0 .. 10]is is an immutable linked list we tell it that we declare a list of numbers 0 through 10, next, let squares stores the result from List.map square numbers. I recommend reading this when it comes to the List.map (http://msdn.microsoft.com/en-us/library/ee370378.aspx).

F# Interactive for Visual Studio

The F# Interactive Console (FSI) is a tool known as a Read-Evaluate-Print-Loop (REPL). This simply means it accepts F# code directly, and compiles and executes it, then prints the results. This allows us to quickly and easily experiment with F# code snippets without the need to create completely new F# projects or build a full application just to print out some results.

To launch the FSI tool simply highlight some code and press ALT+ENTER, the FSI windows will immediately pop-up in Microsoft Visual Studio.

Conclusion

Finally, after playing around with F# and writing this tutorial as part of the tryout, I think I start to like the F# programming language. The F# programming language in my opinion simply provides a better way to deal with different mathematical problems, and solve them. From what I know when I heard of F# several years ago, is that it was proposed to be used by scientists to solve scientific problems, and this is very interesting to me, especially, since I myself am very interested in science and mathematics. In short, I think F# is something worth to learn; sure it takes time and can be painful in the beginning, however, things become clearer as you learn more, so I recommend it and give it a try.

CategoriesF#

Leave a Reply