Archives

Friday, January 16, 2009

F# Language Basics

In this, the second post on F# I am going to walk through a simple application by way of an introduction to the language.

I am assuming that you have been able to download and install F#, which is freely available for Visual Studio 2008.  If not you can download it here: http://research.microsoft.com/fsharp

Creating a new F# project

Open Visual Studio 2008 and create a new project:

 

When creating a new F# project you have 3 options

·         F# Application

·         F# Library

·         F# Tutorial

An F# application creates a complete stand alone application that can be compiled into a standard .EXE file.

An F# Library allows you to create an assembly that you can then use in your other .NET applications.

The F# tutorial offers some pre generated code that allows you to quickly get up to speed with the language.

For this example you need to select F# Application.  Choose an appropriate name and location and then click OK.

Once the solution is created in Visual Studio you will be presented with the source code view of Program.fs which looks like this:

#light


Not particularly exciting and maybe even slightly confusing!

#light should always appear as the first (non comment) line of your application.  The reasons for this are complex and beyond the scope of this post so for now just accept it and lets hear no more about it!

The application that we are going to write will simply apply a calculation to every value in a list and display the results to the screen.  This is deliberately simple to highlight a few of the key concepts of the language.

In Visual Studio, enter the following code:

#light

let square x = x * x

This defines a function called square that returns the square of x.  To a mathematician this makes no sense at all as there is no known value where x is equal to x multiplied by x, however as programmers I trust your still with me!

There are two important things to notice here.  Firstly, the let keyword, which is one of the three most important keywords in F#.  Using let allows you bind a value to a symbol.  This should not be confused with a variable where you typically assign a value to a symbol.

The other important thing here is the lack of any type declaration.  If we were to write this function in C# we would have something like this:

public static int square(int x)

{

    return x * x;

}

Here we have to explicitly specify the type of x as well as the return data.  In F# the compiler infers this information automatically.  This is known as type inference.

Now we need to declare a list of integers from 1 to 15 (or higher if you like) so enter the following code:

let numbers = [1 .. 15]

Lists are the backbone of functional programming and in F# all lists are immutable linked lists.

Now we need to apply the function square to each value in the list numbers and we can do it without a single for loop by using the following code:

let squares = List.map square numbers

Now this is a little more complex so we can break this down as follows.

We are declaring a symbol called squares whose value is the result of evaluating the function: List.map square numbers

List.map generates a new collection whose elements are the result of applying a given function to each element of a collection.  It takes a function and a collection as parameters.

In essence what we are doing here is passing a function as a parameter to another function.  In functional programming, passing functions as values is known as first order functions and is a key concept in functional programming.

At this point your code should look like this:

#light

let square x = x * x

let numbers = [1 .. 15]

let squares = List.map square numbers

If you hover your mouse over squares, Visual Studio will tell you that this is a list of integers.  Type inference at work again!

To display the contents of sqaures we can use the following code:

printfn "Numbers squared = %A" squares

printfn is a simple (and type safe) way to print text to the console.  Where F# differs from C# is the use of a format specifier.  In this example we are using %A which is a general format specifier that prints any type.

Other format specifiers are:

%f – Prints floats

%b – Prints bools

%d – Prints integers

%s – Prints strings

Finally complete the application by adding the following code:

open System

Console.ReadLine()

open System tells the F# compiler to load the System namespace and bring it into scope.  This is the same as writing using System; in C#. 

This code shows how easy it is to use .NET libraries within your F# code and you really can call any library you like.

Console.ReadLine() just pauses the application so that you can see the output before the window closes.

Your finished application should look like this:

#light

let square x = x * x

let numbers = [1 .. 15]

let squares = List.map square numbers

printfn "Numbers squared = %A" squares

open System

Console.ReadLine()

Run the application and you should see the following output:

Hopefully this has given you a little insight into the potential of this language.  Next time I will get a little more in depth as we take on something a little more challenging!

 

No comments:

Post a Comment