Wednesday, January 14, 2009

An Introduction to Microsoft F#

Last night I went to An Introduction to Microsoft F#. It was hosted by the Cape Cod .NET User Group and the speaker was Chris Bowen, a Microsoft Developer Evangelist. Chris was one of the two presenters at the Microsoft Northeast Roadshow in Waltham last month and he will be participating at the Boston MSDN Developer Conference next week.

Chris and Jim O'Neil cover New England as well as upstate NY for Microsoft and they both seem to really know their stuff. Last night was the second time I've seen Chris present and I have also seen Jim twice. They both speak intelligently and with an impressive breadth and depth. They are knowledgeable about a wide range of topics and can give an overview comparison or drill down into the nitty-gritty and go through detailed code with you as the situation requires. Both Chris' blog and Jim's blog are worth watching for upcoming events and generally useful info related to Microsoft technologies. They also both typically post slides and examples from past events they have run, so you can benefit from presentations that you were not able to attend.

F# is a newer .NET language that has come out of Microsoft Research. It is currently available as a CTP and will ship as part of Visual Studio 2010. It is a functional language (as opposed to being imperative such as C# or VB.NET or Xbasic), type-inferred, and interactive. Yes, this means that Visual Studio gets an interactive window for F# where you can play with the full .NET framework. Just as you might use Alpha's interactive window to create an Xdialog and manipulate it for testing/debugging, you can use F# to create a WinForm (or any other .NET object) and manipulate it interactively.

There are 4 main areas that I found most intriguing about F#: concise syntax; curried functions; parallel programming and language orientation.

Concise Syntax
It takes little code in F# to do a lot of work. That alone isn't overly alluring, but this may have benefits in code generation. I won't say much about this because code generation is a pretty narrow topic, but one that I am interested and one that has a role within my current job.

Curried Functions
F# creates curried functions, which means that a function that takes multiple arguments is generated as a series of functions, each taking a single argument, that when chained together creates the desired output. What this provides is an easy way to extend and reuse code. This might be best explained with a simple example. You might define a function to multiply two numbers:
let multiply a b = a * b
If you then call multiply 2 3, the expected result of 6 will be returned. But if you find yourself multiplying an awful lot of numbers by three, you might find a new function to be useful
let triple = multiply 3
So now there is a new function, triple, which exploits multiply. At first glance, this isn't too extraordinary since functions can call functions in other languages. But if you look closer at the definition of triple, you'll see that it doesn't deal with any input arguments at all, which becomes more useful and powerful in a more complex function.

Parallel Programming
One of the implications of being a functional language is that everything in F# is immutable by default (essentially a constant). What falls out of this immutability is near-trivial parallel code.

One of the slides that Chris went over was a small image manipulation routine that modified 1000 (I believe) images in parallel. He first showed the C# implementation, which at 3 pages of code was considerably simpler than I would expect from a C implementation. Then he showed us the equivalent F# code, which was about 10 lines.

There is no need to think about things like threads, mutexes or race conditions with F# because nothing can step on anything else (unless you specifically code it to be mutable). Working asynchronously in many cases is just a one character change. And because F# runs using the .NET runtime, you get the same performance as doing it "the hard way". Chris' C# and F# did the same work in the same amount of time.

One character? Really? Yup. Given serial code:
let longRunningCalculation = ...
It can often be safely made to run asynchronously by adding a single character:
let! longRunningCalculation = ...
Language Orientation
Language Oriented Programming is a vague term that is a bit difficult to clearly explain, and Chris did not say much on the subject so I need to do some more investigation. But basically, LOP allows F# to be extended to support a different language syntax. Again, a topic with narrow interest, but one which has its place on projects I am currently working on.

Resources
fsharp.net - the main Microsoft site for F#. This is where you can download the F# CTP to start working with F# in VS 2008.

Chris highly recommended two books:
  1. Foundations of F#
  2. Expert F#


Additionally, he recommended creating a new VS project using the F# Tutorial Project type after installing the CTP and working through it. He said it gave pretty good coverage of F# concepts and was a good way to get up to speed with the language.

Don Syme's blog - Don is the creator of F# and the author of Expert F#, mentioned above

MSDN F# samples - Chris gave us a quick overview of Samples101, SolarSystem and 3DVisualization. Samples101 is itself an interactive WinForms F# application (a la Learning Xdialog) which demonstrates 101 samples. SolarSystem is a nice example of parallel programming which creates a small window animating the orbit of planets in our solar system. 3DVisualization does some pretty cool graphing using DirectX.

5 comments:

Anonymous said...

What do think about packages like:

Wavemaker
http://ajaxian.com/archives/wavemaker-4-point-and-click-ajax

http://jshurwitz.wordpress.com/2007/11/30/is-wavemaker-the-web-20-version-of-powerbuilder/

and WEBDEV:
http://www.windev.com/webdev/list-of-sites.html

with its wlanguage?

Lenny Forziati said...

I have to admit that I had not seen WaveMaker before now. It looks quite interesting and I will be taking a closer look. I am familiar with WebDev, though I have never used it to build an actual application.

Much like Alpha Five, http://www.alphasoftware.com/, both of those products are RAD IDEs, not a programming language as F# is. Comparing JavaScript, WLanguage, and F# is more appropriate.

These are both procedural OO languages, which is what most developers have been working with for most, if not all, of their careers. True functional programming goes well beyond what JavaScript does (I don't know about WLanguage), highlighted well by the immutability found in F#. It's a bit of a different mindset, but not difficult by any means when you get your head around it.

I think F# shows real promise in its conciseness and its full access to the .NET framework. And I think FP is something we'll all be paying much more attention to in the future.

Anonymous said...

I have been using Alpha for some time, but last weeks I have been evaluating WEB / AJAX IDE's like Morfik, Wavemaker, WEBDEV, VisualWebGUI. The reason is thaty I want to create WEB Apps utilising AJAX wthout really the need to program javascript, anycode whatsoever..

http://www.visualwebgui.com/Home/Introduction/tabid/314/Default.aspx

http://www.jroller.com/sjivan/entry/smartgwt_1_0_released

Thus far I liked: Morfik, WEBDEV, Smartclient and Wavemaker.

There's a lot going on out there on AJAX. I sure hope Alpha will come up soon with something which combines Alpha's Data Driven approach with some of the features of mentioned IDE's or frameworks.

Sorry if the comment does not really relate to F#, but could not find another way to leave this wish...

Lenny Forziati said...

This is my personal blog and doesn't always relate to Alpha, but I'm glad you've found me here and you are more than welcome to post regarding Alpha or anything else.

Without getting into specifics (I try to leave that up to Alpha's marketing team), Alpha has quite a bit up its sleeve to make AJAX easier. V9 admittedly still makes you put in effort to get things working and Alpha aims to change that dramatically.

Ultimately, you should not need to know how AJAX works or even what it is. Instead you should be able to easily build a very rich application that does everything you want which just happens to leverage AJAX or technology abc behind the scenes.

Anonymous said...

Thank you Lenny, that sounds very promising ! I use Alpha to create prototypes at clients to validate the data model and the screen functionality. I have been very succesfull with this approach. The only thing until now was that the performance, because of full pageloads, was at least seconds.

I hope this will be improved dramatically when AJAX techniques can be used (without me having to learn the javascript language or AJAX). Your comment on this re-assures me I should stick with Alpha. Now I only hope Alpha will be able to bring this new version to the market fast. Thanks, Ron