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:
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 22.214.171.124 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-126.96.36.199\bin>fsi MSR F# Interactive, (c) Microsoft Corporation, All Rights Reserved F# Version 188.8.131.52, 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 http://research.microsoft.com/fsharp. NOTE: Bug reports to email@example.com. Enjoy!
> 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
(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.
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.
Angel “Java” Lopez