Interactive Programming with the REPL

In this week’s post I’ll discuss the joys of interactive programming. For those who write only in compiled (or byte code compiled) languages this may be a foreign concept. There seems to be some level of this feature in most scripted languages. In Lisp interactive programming is supported by a feature known as the REPL, or read-eval-print-loop. It is also known as the top-level listener or simply the top level. Ruby has something similar called the IRB, or interactive ruby shell. In Python it’s just the interactive shell.

The REPL acts much like a command shell’s interface but for Lisp. At the top level you’re able to evaluate Lisp expressions. This means defining functions, calling functions, evaluating object values, debugging code, loading code from files and probably some things that I’m leaving out. You can also run quick tests and it makes things easy when learning the language. Wondering how an expression will be evaluated? Check it out in the REPL! Want to see what a function will return with a specific input? Check it out in the REPL!

Lisp and the REPL are inextricably tied together. Although Python and Ruby support this, I think there’s a noticeable culture difference (experienced Rubyists or Pythonists may disagree here). The explanation for this culture of interactive programming in Lisp seems to be Lisp’s focus on functional programming. In “On Lisp” Paul Graham states: “Functional programming means writing programs that work by returning values, instead of by modifying things.” So after you’ve written a new function it seems only natural to run it in the REPL to verify what it returns. Build each function, tie them together, rinse and repeat. This also seems to be a perfect environment to do test driven development. Defining and running tests from within the REPL couldn’t be easier.

All of this worked beautifully when developing some Lisp code last week. I was using the REPL constantly. Learning how things work in Lisp is much easier with interactive programming. If you’re getting tired of all my yapping and just want to play with some Lisp code in the REPL, here are a few things. The first is a pattern matcher I was working on that will match a pattern in a list of Lisp data, which could be Lisp code but that’s the subject of another post entirely.

The second is the test suite for the pattern matcher. How to define the pattern isn’t specified in the comments of the pattern matcher function, so you’ll have to look at the tests to see what’s possible.