Oct 282009
 

The weird looking symbol which follows this post is the F#’s very simple but useful operator – the pipeline.  To sum up what it does in one sentence, I would say it reverses the function call and gives the impression that we are passing the argument to the function. Sounds trivial? Not so much.

One of F#’s advantages is its concise code unlike other .NET languages like VB.NET which are verbose, however this can affect the readability of code especially when you are using nested function calls.

The pipeline operator can reverse this call. For e.g. see this code

// Turn on the lightweight syntax
#light

Seq.iter(fun n-> printfn "%d" n) ((Seq.filter (fun n->n%3=0){0 .. 200}))

Not exactly readable!! what it does is creates a sequence from 0 to 200 and filters it selecting only elements that are divisible by 3. It then iterates through the sequence printing all the values. The fun keyword is an anonymous function, which makes things more clearer and we are spared with the pain of declaring one more function.

Now lets pipeline this code:-

{0 .. 200} |> Seq.filter (fun n->n%3=0) |> Seq.iter (fun n->printfn "%d" n)

As you can see now without the parentheses, its far more simpler to understand this function. Just follow the pipeline. The execution starts with the sequence which gives way to the filter which is then iterated to print the values.

In bigger functions the pipeline is invaluable in making the code readable and more aesthetic. So next time when you are solving that really complex mathematical problem, don’t parenthesize, just pipeline it!!!

Oct 262009
 

In this post we will look at how F# generates IL for a simple program, arguably the first decent program taught to budding programmers after the customary “Hello World”. Yup I’m talking about the Fibonacci series, in which each number is the sum of the previous two numbers.

Here is how the fibonacci series can be implemented in F#.

code

Save this to a text file and compile using the command line compiler fsc.exe “Your file location here” -a . The -a is a shortcut for compiling it into a dll. Lets now dive into the IL of the dll.  Here is how it looks like:-

IL

Notice the node named <StartupCode$fibonacci>. The F# compiler automatically generates this startup code from which all the functions are called. If this was an executable, it would have generated the main method and told the CLR to begin its execution from there. The constructor for this method is fairly simple. It loads the integer that we used for calling the method and then calls our method. If this was a slightly more complex example, you would see how much the F# compiler helps us by writing the scaffolding code.

startup_codeIL

The next function is the actual F# function what we wrote. The argument 0 is loaded on the stack first and then the integer 3 is loaded, A bge opcode is generated next which tells the .NET CLR to branch to IL_0006 in case the greater than or equal to is satisfied. So this check is done the other way round than what we wrote in code. If the condition is not satisfied then it loads 1 on the stack and returns. Else it goes to IL_0006, and the 0th argument is loaded again which is subtracted by 2 by the sub opcode. So now the result of the subtraction is on the stack which is recursively sent  to the same function. Similarly the argument – 1 is recursively called and both of these numbers are added to give the final result.


function_IL

Due to two recursions this method is really slow, anything more than the 30th term will be extremely slow. I tried it out till 30 seconds. Look at the exponential increase in the time taken to do the computation.

chart

IL_0006
Oct 212009
 

First things First. Click here to download Visual Studio 2010 Express edition Beta 2.

When I opened MSDN Forums,  the post announcing  the launch of Visual Studio 2010 Express edition Beta 2 versions was the first on my list . I had asked a question around 4 months ago about when the express editions would be available. The only answer back then was wait.

This is that post by Noah Coad – Program Manager at MSFT.  I rushed to download the express versions, only to find them excruciatingly slow.

Today, we are excited to announce that the Visual Studio 2010 Express Beta 2 products are available for download on the Visual Studio Express Web Site! The Visual Studio Express products are a set of development tools geared towards helping developers unleash their creativity while learning to develop Windows and Web applications.  They offer an easy-to-acquire and easy-to-learn experience in a simple package and they are free!

  • Download Visual Studio 2010 Express Beta2 Today
    Get a jump start on learning to develop on the new .NET 4.0 Framework Beta 2 with the Visual Studio 2010 Express Beta 2 products.
  • Learn more about Visual Studio 2010
    Learn about the many new exciting features in Visual Studio 2010.
  • Try Visual Studio 2010 Professional
    The Beta 2 editions of Visual Studio 2010 Professional and Ultimate are available as free downloads as well.
  • Basic vs Expert Settings
    Unique to Visual Studio 2010 Express … Express now starts up with a streamlined user experience that focuses on the most common commands by hiding some of the more advanced menus and toolbars.  All the Express features are still available via the Tools > Settings > Expert Settings menu.

Also look through the new Start Page inside Express for more resources and information on what’s new.  Enjoy and happy coding!

Noah Coad
Microsoft Visual Studio Program Manager

The only sore point was the missing Express edition for F#. I know that Fsharp interactive is awesome, but was looking forward to a bit more mature IDE especially for trying out imperative programming with F#. Hoping that releasing one is in the pipeline!!!

Oct 212009
 

Since F# is a functional programming language it needs to have robust data handling capabilities. And it doesn’t disappoint in this regard. In this post, we will look at how F# works with Sequences.

One of the data types which represents Sequences is Seq<type>. This F# datatype maps to System.Collections.Generic.IEnumerable<type>. As with all the sequences, Seq<type> can be iterated over. To generate Sequences we can use the keyword seq followed by the range.

For e.g. typing seq { 0 .. 2};; in F Sharp Interactive (FSI) yields the results

sequence

One great thing about F# that I mentioned in my previous post as well is its automatic type inference. So If I give a decimal number, it will automatically evaluate the data type. In the below example it decided that the datatype of the sequence should be float.

seq2

A sequence from 0 to 5 is created with an increment of 1. This is because by default the increment is always one. To change this we need to specify the increment while defining the Sequence. For e.g. the following expression creates a Sequence from 0 to 10 with an increment of 2.

seq3

Note that only the first few elements are shown while evaluation. This feature is called Lazy evaluation. Basically it computes the whole sequence only when forced to do so. In our case, to display the evaluation, it just shows the first 3-4 numbers followed by the two dots. Suppose we do some operation on the sequence which involves iteration, then the other elements will be evaluated.

Speaking of iterations, we can do so over a sequence with the for .. in keyword in F#. It is similar to the foreach construct of C#. A small e.g.:-

seq4

Appending Sequences:-

As the name suggests, this method appends two sequences together. In the below code example, we declare two sequences,seq1 contains the odd numbers from 1 to 9 and seq2 contains the even numbes from 0 to 10. Then a function printSeq is declared which takes in a seq as an argument and prints the whole sequence.

A new seq3 is declared which is formed by appending seq1 and seq2 and the contents are printed. The contents of this seq3 are printed using printSeq.

seq5

Theres a lot more to sequences than what I have given here. Will follow up with more posts on this topic.

Oct 192009
 

When .NET 1.1 came out with Visual Studio 2003, there was support for four languages VB.NET, C#, Managed C++ and J#. J# was a java clone which Microsoft had introduced with the hope of pulling more developers from Sun Java to the .NET framework. It stayed in VS 2005 as well. In Jan 2007, Microsoft announced that they are formally retiring J# to better allocate resources for other customer requirements. In Visual Studio 2008 , J# was conspicuous in its absence.

There was a noticeable void in Visual Studio 2008 with the exit of J#. .NET’s primary strength is its Interoperability, the ability to code in a language of your choice, in a syntax of your choice and yet be compatible with all existing code. This choice was just decreased a little bit. In came F# to fill the void. In Visual Studio 2010, F# will be one of the integral language options. To give a one liner about the origin of F# – it derives from the language OCaml and also has its roots in Haskell.

So whats so special about F#  that it grabbed so many developers eyeballs? For me it was the syntax. Its a functional programming language which can not only be used to solve computational problems, but also be used in developing conventional applications with the same amount of ease as C# or VB.NET.

To quote Don Syme :- F# differs from many functional languages in that it embraces imperative and object oriented (OO) programming. It also provides a missing link between compiled and dynamic languages, combining the idioms and programming styles typical of dynamic languages with the performance and robustness of a compiled language. The F# designers have adopted a design philosophy that allows you to take the best and most productive aspects of these paradigms and combine them while still placing primary emphasis on functional programming techniques.

F# differs from many functional languages in that it embraces imperative and objectoriented
(OO) programming. It also provides a missing link between compiled and dynamic
languages, combining the idioms and programming styles typical of dynamic languages with
the performance and robustness of a compiled language. The F# designers have adopted a
design philosophy that allows you to take the best and most productive aspects of these paradigms
and combine them while still placing primary emphasis on functional programming techniques.

It is understandable that every designer would praise his own language in flowery words, but in the case of F#, I found that most of it to be true. The automatic type inference, functions within functions, very short syntax is where F# scores over other .NET  languages. The built in data types of Seq, List, Map are awesome ways of handling data and allowed me to get through some common mathematical problems in just 2-3 lines of code.

The best part is the compiler, In absence of a Visual studio express version being available, MSFT has released an interactive compiler called FSI (F# Interactive) which dynamically evaluates code. Yes, you heard it right, you can force it to evaluate each and every line of code, especially useful in a language with automatic type inference. I wish something like this was available for C# when I was learning it 🙁

In short F# gets a thumbs up from the developer community (at least me ). Will come up with some more articles on common F# types and functions.