First steps in F#

The F# language has been under development in Microsoft Research. Its main creator is Don Syme. You can visit the F# web site:

A comment from that site:

Combining the efficiency, scripting, strong typing and productivity of ML with the stability, libraries, cross-language working and tools of .NET.

F# is a programming language that provides the much sought-after combination of type safety, performance and scripting, with all the advantages of running on a high-quality, well-supported modern runtime system.

The last published version, up today, is:

F# realease announcement

Installing F#

You can download a .msi file, or a .zip one. The .msi is an installer program, that install F# compiler, and F# for Visual Studio. I installed the version in my machine. This is one of the screen from my past year installation:



The last step could take many minutes. It install the support of F# inside Visual Studio. You can use VS or, alternative, a F# command line compiler and interpreter. The F# programs can be launched from Mono, then you can write F# code under Linux.

At the end of install, a folder is created c:\Program Files\FSharp-<version> (it has a .sh file to install the tools in Mono).

There is a new program menu:

The fsi interpreter

We can use an interactive tool, that is the F# interpreter at bin\fsi.exe:

C:\Program Files\FSharp-\bin>fsi MSR F# Interactive, (c) Microsoft Corporation, All Rights Reserved F# Version, compiling for .NET Framework Version v2.0.50727 NOTE: NOTE: See 'fsi --help' for flags NOTE: NOTE: Commands: #r <string>;; reference (dynamically load) the given DLL. NOTE: #I <string>;; add the given search path for referenced DLLs. NOTE: #use <string>;; accept input from the given file. NOTE: #load <string> ...<string>;; NOTE: load the given file(s) as a compilation unit. NOTE: #time;; toggle timing on/off. NOTE: #types;; toggle display of types on/off. NOTE: #quit;; exit. NOTE: NOTE: Visit the F# website at NOTE: Bug reports to Enjoy!

Using let

Let’s enter

> let x = 10;;

We get at output:

val x : int

The ;; makes the program to compile and execute the entered commands. In this command, x is an identifier, that has an integer value. F# is a typed language. We didn’t define the type of the identifier x: it was inferred automatically from the value we put in it. Let’s note that is an identifier, not a variable. Once an identifier is set, we can’t change its value. That is a distinctive feature of functional programming in general. I’ll explore that behavior in future posts.

Now, let’s enter

> x;;

(the > was added by the program, it’s the fsi’s prompt) We get

val it : int = 10

The fsi program prints the expression and its type. it is the name that it uses when we don’t speficy a target identifier.

Defining functions

Using the same let, we can define functions:

>  let dup x = x * 2;;
val dup : int -> int

For F#, values and functions are the same. A function is a first class citizen in the F# language, and it can be manipulated as any other value.

The second line above shows that dup is a function that receives an integer and returns another integer. The identifier dup now points to that function. It is its value.

We can apply the function entering:

> dup 10;;
val it : int = 20

A function is a value, we can pass a function as a parameter:

> let apply f x = f (f x);;
val apply : (‘a -> ‘a) -> ‘a -> ‘a

The result is something more cryptic. The term ‘a refers to a undefined type. It could be an integer, or an string, or something else. But if we give an integer as second parameter to apply, the first one MUST BE a function that takes an integer and returns another integer. Don Syme, the creator of F#, was involved in the definition and implementation of generics in .NET 2.x. One could think he implemented that features to create the F# language… 😉

If we entered

> apply dup 2;;

then we get

val it : int = 8

that is the same that

> dup (dup 2);;


For those who are not familiar with functional programming, the concept of a function as a first class value, citizen with the same rights and duties of a value, could sound something strange. But if you can grasp the base of this approach, you’ll appreciate the advantages of this path (the appearance of lambda and delegates in mainstream .NET is an example of such adoption). If you have seen a Lisp or other functional implementation, all this sounds as a deja vu for you.

The interesting part of F# is that not only supports the functional programming paradigm but also interacts with the rest of the .NET framework. Then, F# is not an “academic” language, only related to itself, but was adopted in many projects. Although I was dealing with the command line interpreter in this post, we can use the Visual Studio, leveraging debugging, projects, and editors.

Enjoy F#!

Angel “Java” Lopez

One thought on “First steps in F#

  1. Pingback: Closures in F# « Angel “Java” Lopez on Blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s