Click here to monitor SSC

Laila Lotfi

.NET tools Brand Manager & Simple-Talk Editor

Functional Programming for the Working .NET Programmer

Published Monday, July 20, 2009 4:46 PM

We've already had some emails sent to us with a gentle reproach that we are not giving enough coverage to the F# language (F for Functional). F#, if you're not familiar with it, is likely one of the great pleasures of Visual Studio 2010. It started as an open research project that adapts the classic functional programming language ML, via OCAML and Haskell, to enable it to use NET assemblies. It is now out as a fully-supported language in Visual Studio 2010 Beta1, and as a plug-in for as a fully-supported language in CTP Update for Visual Studio 2008 in the form of the F# May 2009 CTP (MSI, ZIP).

Anyone who has used ML, Haskell or Caml will recognize F#. Even Phil Factor was young when ML appeared at the University of Edinburgh in 1973. Functional programming is quite unlike the more common imperative model. It is oriented around expressions, as it consists of functions operating on simple data structures and avoids the use of variables (as generally understood) or operations with side effects. This ensures that the same function with the same input will always produce the same output (deterministic). Functional programming has evolved in parallel with imperative programming (e.g. Pascal, C#) and declarative programming (e.g. SQL). It is the ideal language for slow typists. Algorithms are cleanly expressed without having to wrap stuff up in fluff such as class definitions, and without having to type everything, since the system does that automatically for you.

It has survived the lean years by being the ideal advanced teaching language but has been used commercially for a variety of purposes that rely on advanced analysis techniques, particularly in financial systems. It has also been adopted by many as the ideal language for writing compilers. By adapting OCAML, a dialect of ML with Objects, to support .NET types and objects, Don Syme and his team at Microsoft has necessarily compromised the purity of ML by giving support for NET types and objects, using an imperative object-oriented style of programming.

Please don't get the impression that F# is an academic language. It is a well-proven, general-purpose language with a power that is equal to C#, but which is fundamentally designed for parallel processing. It is economical too. A script to manipulate an Excel spreadsheet or copy a database via SMO is even shorter than the C# equivalent. What is more, it is interactive, and adopts a programming paradigm that is surprisingly intuitive for any SQL Developer, with its built-in support for tuples, record and lists. It is in other senses quite unlike the more common languages as it is both functional and imperative. You can build F# applications for .NET 2.0/3.0 and 3.5 and 4.0

The classic textbook on ML is Lawrence Paulson's ML for the working programmer. The title was, one suspects, originally an ironic comment on its practicality for commercial use, rather than the theoretician in logic. The title no longer seems ironic, since, with its descendent F#, we have a tool that is of immediate use for any .NET programmer. We'd love to publish some articles on F# by experienced 'working programmers'.

Cheers,

Laila
by Laila

Comments

 

Charles Kincaid said:

This sounds great for many things.  My hope is that they get this pushed to us Mobile developers.

Charles Kincaid
July 22, 2009 9:46 AM
 

grahamsw said:

First off, F# is great, I absolutely love it. I'd recommend it to anyone with a passion for programming (who works in a MS environment). Take the time, learn it. Even with relatively little knowledge it is phenomenally useful. Programs that would be 100s of lines of C# come out as 10 lines of F#. For hard core data processing it's a dream. To have a functional language completely integrated into .NET- so I can actually use it is fantastic.

Now for the other side of the coin.

I'd had a little experience with functional programming (after 15 years with the C/C++/C# progression. C++'s STL was some sort of preparation, and I'd read Structure and Interpretation of Computer Programs, and spent a little time with Haskell) I'm aware that some of my F# problems involve unlearning other habits, but not all of them. But I started learning F# about 18 months ago and I still feel like a beginner.

F#, to me, suffers quite badly from Perl's "there's more than one way to do it" problem. There are lots of ways to do practically everything. This might be a lot of fun, but it means you need to decide which approach to take, and to learn them all, and to decipher them when you read someone else's code. Also, its terseness can tempt one down the treacherous path of writing clever code.

For all that, it is wonderful to have in the .NET family. (Thanks Don.) It isn't suitable for everything, but for implementing algorithms it's a dream.
July 22, 2009 2:51 PM
 

Tony Davis said:

In a fascinating interview in this issue of Simple-Talk, Don Syme discusses his work on the F# programming...
January 21, 2010 8:50 AM
You need to sign in to comment on this blog

About Laila

I'm working for the .NET Tools division at Red Gate. These days, I'm spending most of my time working on SmartAssembly (.NET obfuscator, error reporting and feature usage reporting tool), and I'm currently looking to speak to anyone who has used the product for its error reporting technology. My email address: editor@red-gate.com
<July 2009>
SuMoTuWeThFrSa
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678
How to Kill a Company in One Step or Save it in Three
 The majority of companies that suffer a major data loss subsequently go out of business. Wesley David... Read more...

Migrating from OCS 2007 R2 to Lync: Part 4
 Having migrated the rest of our users and legacy resources across and started getting ready to... Read more...

Automated Script-generation with Powershell and SMO
 In the first of a series of articles on automating the process of building, modifying and copying SQL... Read more...

Seth Godin: Big in the IT Business
 Seth Godin has transformed our understanding of marketing in IT. He invented the concept of 'permission... Read more...

Using SQL Test Database Unit Testing with TeamCity Continuous Integration
 With database applications, the process of test and integration can be frustratingly slow because so... Read more...