Something New Under the Sun

What do Haskell, F# and OCaml have in common? They’re functional programming methods that can infuse your programming with new ways to do things.

Learn at least one new language every year. Different languages solve the same problem in different ways. By learning several different approaches, you can help broaden your thinking and avoid getting stuck in a rut.
— Andrew Hunt and David Thomas, The Pragmatic Programmer

You probably learned procedural programming first. And now you’re an object-oriented genius. But have you ever looked into functional programming? Functional programming is one of those concepts that most of us have never needed to master. But with all the resources of the Internet at your fingertips, it’s pretty easy to get started with a new language these days. And if you’re a serious developer, you already know that learning new languages can turn out to be useful in unexpected ways. So, I’ll look at functional programming, starting with the Haskell language.

Everything You Know Is Wrong
Suppose you set out to write a programming language that made none of the assumptions that you’re used to from C, C++, Visual Basic, C# or Java. You might well end up with Haskell. Here are a few features of Haskell you may find puzzling:

  • Identifiers don’t change their values. If x has the value of 2 in a Haskell program, it has the value of 2 throughout the program.
  • A Haskell program tells the computer what to do, but not what order to do it in. The computer gets to decide in which order the statements should be executed.
  • It’s easy to define (and sensibly use) a list with an infinite number of elements.
  • Side-effects of executing a function are impossible.
  • Haskell has no looping constructs (though there are ways to fake it). It uses recursion instead of loops in many cases.

OK, so why in the world would you use such an Alice-in-Wonderland language? Well, here are a few of the advantages that Haskell proponents mention:

  • Increased productivity
  • Shorter programs
  • Fewer errors
  • Ease of understanding

Well, that all sounds kind of yummy, doesn’t it? Just to whet your appetite a bit, here’s a tiny bit of Haskell code. It’s an implementation of quicksort from the Haskell Web site, http://www.haskell.org (which has an absolute wealth of Haskell resources: everything from book-length tutorials to free compilers):

qsort []     = []
qsort (x:xs) = qsort elts_lt_x ++ [x] ++ qsort elts_greq_x
                 where
                   elts_lt_x = [y | y <- xs,="" y=""><>
                   elts_greq_x = [y | y <- xs,="" y="">= x]

The idea here is simple: To quicksort an empty list, just return the empty list. To quicksort a longer list, split it into the first element and the rest of the list. Return the result of quicksorting everything less than the first element, then the first element, then the result of quicksorting everything greater than or equal to the first element.

Developer Central Newsletter
Want to read more of Mike's work? Sign up for the monthly Developer Central e-newsletter, including product reviews, links to web content, and more, at http://lists.101com.com/
NLS/pages/main.asp?NL=
mcpmag&o=developer
.

Not only is this a very parsimonious implementation in terms of code, but it’s also very close to the actual quicksort algorithm. That’s one of the beauties of functional programming—you just put the algorithm straight into the code, and you’re done. Of course, there is a whole lot more hung on top of this idea, including specialized ways to handle I/O and exceptions, libraries of pre-existing functions, list and tuple manipulation, and plenty more.

And Haskell is good for more than CS courses and parlor tricks. Haskell has been used for prettifying C++ source, designing FPGA circuits, natural language processing, and rendering OpenGL graphics. There’s even an Asteroids clone written in Haskell.

Functional programming isn’t really as strange as that list of features I listed earlier. Excel worksheets, XSLT stylesheets, and SQL statements are all examples of specialized bits of functional programming, where you specify what to do and let the computer worry about how to do it.

Getting Out of the Box
One problem with learning a new language is hooking it up to existing code. Most of us don’t care to abandon an entire toolbox full of carefully hoarded code snippets and libraries that we’ve built up over the years, just to play with the latest and greatest toys. Fortunately, an explosion of open APIs over the past decade has made this less of an issue than ever before. It seems like everything is hooked up to everything else, and Haskell is no exception. Here are a few of the glue bits that you can grab for this language:

  • HaskellDirect is an IDL compiler for Haskell. Thus you can call COM objects from Haskell or create COM objects in Haskell to be called by other COM applications.
  • GCJNI is one of several Java Native Interface implementations for Haskell.
  • HaskellScript ties Haskell into Microsoft’s active scripting framework.
  • Haskell/CGI lets you write CGI scripts in Haskell.
  • haskell-corba lets you write CORBA clients and servers in Haskell.
  • HaSQL is a Haskell-to-ODBC interface.
  • MySQL-HS ties Haskell directly to MySQL.

You get the idea. With free tools you need to be careful to set aside time for testing, to make sure that what you’re trying to do is supported. But on the flip side, you can usually get the source code to add the support yourself if you must.

One More Letter Down
Haskell is not the only fish in the functional language sea. You’ve heard of C# and J#, and perhaps even of the rumored future X# XML-based .NET language. But how about F#? F# is a functional language for .NET, originating at Microsoft Research. Remember, this is the “blue-sky” part of Microsoft. Some things from MSR ultimately end up in products; many others do not. So don’t count on seeing F# in the next Visual Studio .NET package. But do take this as a sign that functional programming isn’t as far from your computer as you might think.

You’ll find the F# homepage at http://research.microsoft.com/projects/ilx/fsharp.aspx. (Browse around research.microsoft.com some afternoon when you’ve nothing else to do; there are some fascinating tidbits there, as well as various projects to make you wonder how some people manage to continue collecting a salary). F# is a .NET implementation of the core of the OCaml language, which combines functional, object-oriented, and imperative features into one giant, confusing package. Actually, it’s not all that confusing. OCaml source code is pretty easy to read, though there are quite a pack of concepts to learn.

So, maybe the notion of a functional language in the .NET environment isn’t so far-fetched after all. If there should be a sudden burst of popularity for such languages, it wouldn’t take much (beyond a few zillion programmer hours, which they have) for Microsoft to turn F# into a product.

Pick the Best Tool for the Job
Perhaps you’ll never need the capabilities of Haskell or F# or OCaml for any project that you’re involved with. But if you don’t develop at least a rudimentary understanding of those capabilities, you’ll never know. As the pragmatic programmers suggest, the more computer languages you’re familiar with, the better the chance that you’ll come up with an elegant solution to any given software problem.

There’s another reason for studying new languages, of course. In an environment such as .NET, you can easily mix and match modules written in different languages. Those developers who are familiar with the widest range of .NET languages are the ones who are best positioned to take advantage of this rich integration and, thus, the most salable. Along with keeping up your certification credentials, remember to keep up the underlying knowledge credentials. The combination will serve you well as you compete for employment or freelance contracts.

Haskell too academic for you? Boss won’t let you waste time developing new skills? Or do you have a functional programming success story? Drop me a line and let me know about it! I’ll use the most interesting comments in a future issue of Developer Central.

About the Author

Mike Gunderloy, MCSE, MCSD, MCDBA, is a former MCP columnist and the author of numerous development books.

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.