Building Interactive Web Programs with Continuations


Supposed I asked you to build a program to grab the current exchange rates from the Federal Reserve Bank in New York (FRBNY) in XML, prompt the user for the currency to exchange dollars for, then prompt the user for the dollars to convert, and then display the result. You might write a program that looks like this:

  1. get the rates from the Federal Reserve Bank
  2. show the first prompt and record the currency choice
  3. show the second prompt and record the dollar amount
  4. query the XML to extract the right information using the user input
  5. calculate the result and display it

In fact, other than grabbing and querying the XML, any beginning programming student could write this program. You could probably do it in under 5 minutes in your favorite language.

Now, suppose I asked you to convert your program to a Web application. Still a five minute job? Hardy, and the increase in time is more than just the problem of building the Web pages. What you'd probably do is put each of those steps into a separate script and then bind them all together with a little state machine in the CGI program that jumped to the right script based on state variables that you encoded in the URL, a hidden field, or a cookie. Sadly, it wouldn't end up looking anything like the program that you'd write if you were just grabbing the input from a terminal.

The problem with programming Web applications is two-fold:

  1. The data from each page has to be managed independently and stored in a persistent manner that lives beyond execution of the CGI script.
  2. The state of the program has to be managed explicitly by the programmer.

When you think about it, Web programming is a lot like assembly language programming--lots of gotos and ad hoc mechanisms for managing data. Dave Weinberger wrote a book called "Small Pieces Loosely Joined" to describe the Web. The problem is that that's just what Web-based programs look like as well. Dijkstra must be spinning in his grave.

This leads to the question: what if I could write programs for the Web that were "structured" in the programming sense of that word? The result would be Web programs that were more natural to write and easy to read. You'd no longer have to maintain the state of your program outside the language and the data could be kept in variables, where it belongs. The answer is: you can.