Monday, April 19, 2010

Functional Programming, a perfect fit for Web Programming?

The last few months I've become really interested in Functional Programming. I've learned Haskell, Scheme, and F#. I've also looked at how to do Functional Programming in Imperative Programming languages like Python, Java, C and C++.

I've been doing that as a hobby on evenings and weekends, as I'm only using Java at work. Evenings and weekends have added up and I've actually done quite a bit of real functional programming in Scheme and F# now, as well as some functional 'style' programming in Python and C++ in weekend contributions to the Apache Tuscany C++ SCA project.

I'm only starting to digest that Functional Programming experience.

I've also done Web programming for 15 years and have been lucky to work on projects using so many Web frameworks and languages that I can't remember all of them... Well, I still remember when I almost gave up on programming altogether, out of frustration as I was trying to do something like... AJAX in 1996... and Internet Explorer and Netscape were driving me crazy and crashing all the time :). We didn't have a fancy name like AJAX at the time, just dynamic HTML and Javascript.

I'm not sure I've completely digested Web programming yet, but I've got over a number of good and bad, or let's say 'learning', experiences.

Last week I was bored and I started to think about similarities between Functional Programming and Web Programming. I thought about a few fun analogies:
  • Start with an HTTP GET That looks to me like the application of a function 'add', with precise input and output. An idempotent function with no side effects. Like with Functional Programming, the result can be produced by executing code (a Python script for example), or just a table lookup (a pre-generated page for example.)

  • Use a URL like That looks less that the location of a script with parameters and more like the location of a resource, or data item. It's starting to blur the line between function application and access to data. Like with Functional Programming, Web functions and data seem interchangeable.

  • Do an HTTP redirect or use a URL shortening service to shorten into for example, and you have function currying.

  • Use a caching proxy server or use client side caching based on last-modified of etags headers, and you're doing function memoization.

  • The HTTP POST, PUT, DELETE verbs introduce precisely identified side effects. That's what you're looking for when you do Functional Programming. When you can't avoid them, you want to be very explicit and precise about the side effects introduced by your code.

  • Return an HTML page. That looks like a value representing a computation, which will perform some I/O in your Web browser at the edge of the system. Will functional programmers see the analogy with an I/O Monad?

  • HTTP is stateless. When you pass state around across multiple HTTP interactions, wrapped in additional hidden fields or parameters on your URLs, are you implementing a State Monad?

  • Finally, could Functional Reactive Programming and lazy event streams help you implement AJAX callbacks, subscribe to ATOM feeds or Pubsubhubbub updates for example?

Something to think about... What if Functional Programming was a perfect fit for Web programming? What if you could compose Web functions like functional programmers compose functions?

No comments:

The postings on this site are my own and don’t necessarily represent positions, strategies or opinions of my employer IBM.