Friday, April 14, 2006

Functional Programming for the Masses - Are We Ready For It ?

I have been talking a lot about Scala of late. Scala is a multiparadigm language offering both OO and functional programming features. This post is not about Scala though. It is about the latest trend in modern language design - the return of the functional programming in the mainstream. We have Ruby, Python, Scala all of them enriched with higher order functions, type inference, initializers, lambdas, meta-programming with expression trees and many other features which have their roots in functional programming languages like Lisp and Haskell.

But guess who is the leader ? The big daddy, of course. Microsoft has unleashed the LINQ framework which will be supported by upcoming versions of C# 3.0 and Visual Basic 9. Erik Meijer has hit it right on the nail in LtU - Functional Programming Has Reached The Masses - It's Called Visual Basic. In his confessions, Erik notes
After a long journey through theoretical computer science, database theory, functional programming, and scripting, abstract concepts such a monoids, lambda-expression, and comprehensions have finally reached the day to day world of ordinary programmers. The LINQ framework effectively introduces monad comprehensions into the upcoming versions of C# 3.0 and Visual Basic 9.


LINQ defines query operators for declarative traversal, filter, projection operations over abstract collections (based on IEnumerable<T>). The query interface extends seamlessly over both XML and SQL data as this overview document from Microsoft states
The query operators over XML (XLinq) use an efficient, easy-to-use in-memory XML facility to provide XPath/XQuery functionality in the host programming language. The query operators over relational data (DLinq) build on the integration of SQL-based schema definitions into the CLR type system. This integration provides strong typing over relational data while retaining the expressive power of the relational model and the performance of query evaluation directly in the underlying store.


In today's application development environment, we use Relational databases for persistence, Objects for implementing business logic and XML in the presentation tier - the infamous ROX triangle of Erik. By offering all of the three paradigms within the same framework, LINQ promises to unify all the three data models for the application developer. Under the hood its all monads and comprehensions, however, the application programmer uses his favourite SQL like syntax

Dim expr As IEnumerable(Of String) = _
    Select s.ToUpper() _
    From s in names _
    Where s.Length = 5 _
    Order By s


Along with .NET providing seamless functional programming capabilities uniformly over C# 3.0 and Visual Basic 9, modern languages like Ruby, Python and Scala also have lots to offer in the game. Programmers at large are getting to see the daylight of the elegance of functional programming through the monads, closures, meta programming and comprehensions. Despite the fact that each of these have been around for years, locked up in the functional programming community and discussed by a handful in LtU, they are expected to be part of mainstream programming very shortly. The question, however, is that, is the programming community ready for this ? In one of my earlier posts, I had expressed the concern over the lack of knowledge on the basic principles of functional languages like recursion, higher order functions, function currying, closures amongst the myriads of programmers coming out of CS schools. I think it's time the grad schools shell themselves out of their Java clothing and go back to the first principles that SICP has preached for ages.

1 comment:

Anonymous said...

I can tell you first hand coming from the Java world that understanding some of the ideas behind functional programming can be tough... Monads in particular have been giving me a hard time. Things are starting to come clear after reading things like this:

http://homepages.inf.ed.ac.uk/wadler/topics/monads.html

I'm still not 100% sure that these ideas will go mainstream... I need to read that article to see how much is exposed to the developer with LINQ. Function programming, etc. is definitely a different world from the regular corporate developer paradigms that have become popular, so we'll see how it goes over.