Scheme is not one Language

July 13, 2009

Considering all the hassle of using multiple languages in one project, was it really a good idea to use Scheme? I would say so.

Scheme is not like other languages. In a way, Scheme is not really one language, but a foundation for defining other languages. The semantics of Scheme were designed to enable programmers to devise their own control structures and the syntax was minimized to make it easy to parse, modify and layer your own syntax on top of. The language as a whole left so many other things unspecified that you must build your own environment on top of it unless you’re working on a small project.

One example of the power Scheme gives its users is unlimited continuations. A continuation is a point in the execution of a program which has been lifted up to the language layer, where it can be passed around or saved in variables, letting you execute it again later should you want to. That was pretty abstract so I’ll give you an example:

As you may know, functions in C programs push their return address on a stack before they are invoked. When a function returns, it pops the return address from the top of the stack and jumps to that address, which is where the call came from. The return address is the continuation of the caller (I’m simplifying here), but it doesn’t exist as a first-class entitiy in the C language, so we cannot see it or refer to it. In Scheme you can. Moreover, you can call a continuation as many times as you wish, and you can ask for the current continuation at any point, not just at call sites.

This is not terribly useful in day-to-day coding, but it is essential if you want to make your own language. Using continuations, you can implement exception-handling mechanisms like try-catch without resorting to low-level hackery (and things like loops with complicated exit logic and light-weight threads, too).

What all this comes down to is that Scheme is a very good environment for language experimentation, which has resulted in a lot of people hacking on these kinds of things. Consequently, you can go to the web page of your Scheme environment and download language extensions others have written that add or alter fundamental parts of the language; They may for instance add object-oriented constructs and run-time dispatch. Mainstream languages are much less malleable.

In summary, I think the ability to download language extensions and, so to speak, assemble my own language from pieces written by others has been a huge advantage. That, and the ability to make changes to the program as it’s running has been more than enough to justify the hassle.

Leave a Reply