2012
09.10

Falling in Love to F#

F# is a functional programming language that recently officially added to Visual Studio.NET supported languages. To me it reminds me alot to Python because of the indentation rules. If you want to play around with this language, you could either use:

  1. Visual Studio 2010
  2. Visual F# 2011 CTP
  3. Visual Studio 2012 Developer Preview
  4. Your browser

What made me attracted so much to F#? It was caused by this informative, interesting and humorous session by Luca Bolognese. And yes, since he’s an Italian, his introduction to programming in F# is an offer that I can’t refuse! 😀

In his talk, Luca highlighted three most important keywords in F#. They are:

  1. let. This keyword is similar, but different to var in C# or Dim in VB. But let is actually more on binding one object/value to a symbol. This symbol will be an immutable object, which means the its value/object can’t be change during its life-cycle.
  2. |>. This operator is similar to pipe in MS-DOS.
  3. fun. This keyword is used in creating lambda function.

Luca also highlighted that F# gives you the facility to immediately concentrate on the core solution of the problem. Because in F#, Luca said, we can program the solution of a problem just like the way we would solve it in our brain.

Anyway, let’s get dirty with simple example in F#:

//bind x to an integer
let x = 10;

//create a list of integers
let data = [1;2;3;4]

//define a square function
let sqr x = x * x 

//Luca notes that this method is something that we will do 
//in C#
let sumOfSquaresI nums =
    //"let mutable" will create a variable instead of binding to a symbol
    let mutable acc = 0
    for x in nums do
        //assign value to acc
        acc <- acc + sqr x
    //return the value
    acc

//This is the same function, but in a recursive function
let rec sumOfSquaresF nums =
    //match is a branching mechanism plus binding
    match nums with
    | [] -> 0
    //h::t will match to a list 
    //and bind symbol h to the first element of the list
    //and bind symbol t as the rest of the elements in the list
    | h::t -> sqr h + sumOfSquaresF t

//This is the F# way of doing Sum of Squares
let sumOfSquares nums =
    nums
    //Operator |> is similar to a DOS pipeline
    //Seq.map will apply the function sqr to each elements in the list
    |> Seq.map sqr
    //By now the list we have is a list of squares, e.g. [1;4;9;16]
    //Seq.sum will sum every elements in the list and return an integer
    |> Seq.sum

From above code, I hope we can quickly spot the difference between programming in imperative way (sumOfSquaresI) vs. the F#-way (sumOfSquares). Clearly, the F#-way gives us not many room to make mistake, and if you think of it, it is indeed how we would solve it in our brain (first, square every element in the list, then sum it up).

Another plus point is parallelization. Parallelization is very much working out of the box in F#. We can easily parallelize the function sumOfSquares by calling the parallel-version of the method.

let sumOfSquares nums =
    nums
    |> Seq.pmap sqr
    |> Seq.psum

GD Star Rating
loading...
Falling in Love to F#, 3.0 out of 5 based on 1 rating

Incoming Search Term

Advertise Here

1 Trackback

 

No Comment

Add Your Comment