As I wrote on Saturday, I’m learning to program in Clojure; and being a tool user at heart I immediately went looking for the right tools for the job. Here’s what I wanted:
- A REPL at which to experiment with Clojure code, with good command-line editing and command history.
- An editor in which to edit Clojure code, integrated with the REPL.
- An easy way to build a stand-alone application in Clojure.
- A solution that works on both Mac and Windows.
“REPL” stands for “read-eval-print loop”, and it’s a LISP-ism for a terminal-style interface to a language. You type a command at the language interpreter, it reads it, evaluates it, and returns the result. Then you type something else, and it responds to that, and so on. It makes it possible to develop programs interactively, and test your code as you write, and it’s very cool.
user=> (defn square [x] (* x x)) #'user/square user=> (square 5) 25 user=>
LISP programmers like to talk as though they invented the REPL, which they did, and they keep using the term “REPL”, which most people don’t; they just call it an interpreter. (And then they tell you that it isn’t really interpreted, it’s all compiled under the covers.) The first programming language I learned was BASIC on a PDP-11; it was an interpreter, and when you started it up you got a command prompt and developed your program interactively. REPLs went into a decline about the time BASIC lost its line numbers, but have since resurged; and most of the new languages I see have something like one.
Anyway, in order to learn Clojure properly, you need a REPL so that you can experiment and see how the language works. The easiest way is just to download the Clojure “.jar” file, and use Java to execute it. For example, I can go the Terminal app on my Mac, switch to a folder that contains the Clojure “.jar” file, and type this at the command line:
% java -cp clojure.jar clojure.main Clojure 1.6.0 user=> (+ 1 1) 2 user=>
It’s a perfectly good REPL; except that it’s perfectly unusable to anyone used to using this kind of terminal interface. Almost every program that provides a command prompt these days has some variant of what’s usually called “readline” support: you can use the Left and Right arrow keys to edit the line you’re on, and you can use the Up arrow key to recall previously entered commands. As a programmer, I live at the command line, and my muscle memory expects those things to just work. In this REPL they don’t. Instead, you get funny control characters which do nothing good for your code.
Consequently, I needed something better.
The first program I tried is the aptly named Light Table. Light Table is an attempt to completely rethink the traditional programmer’s IDE, giving instant and immediate feedback about the state of the programmer’s application even as he’s coding it. Light Table is written in a variant of Clojure called ClojureScript, and has built-in support for Clojure editing; it also has a built-in “InstaREPL”. It runs on Mac and Windows and Linux. So I downloaded it and gave it a try.It’s…interesting. Most programmer’s editor’s these days do something called syntax coloring, to make the syntax of the language stand out, and Light Table does it a little differently than most:
LISP by its nature has lots and lots of parentheses; Clojure has fewer, but makes up for it by having many more brackets and braces. And they all have to come in nested pairs, which can get confusing. If you look at the screen shot just above, notice that the parens, brackets and braces are colored based on how deeply they are nested. This is very cool, and is Light Table’s best feature in my view.
The InstaREPL, on the other hand, is not so good. It tries hard, mind you. Here’s what it looks like:
It isn’t like a normal REPL, where the user and the language take turns in a kind of dialog. As soon as you start typing, Clojure is trying to make sense of it. And when it makes sense, it shows the result to the right of what you were typing. Above, for example, you see “4” to the right of “(square 2)”, which makes sense. On the line above, you see “2 2 2”, which appears to be the value of variable “x” each time it appears in the definition of “square”, for the last thing you entered. This is the “light” in “Light Table”: you can immediately see what your code is doing.
I wanted to like Light Table a whole lot. But there’s something about how the InstaREPL works that confuses me. Every time I’ve tried to use it, I’ve somehow ended up confusing it, or me, or both, such that the code I’m typing in doesn’t work when it should. I spent forty-five minutes trying to figure out why something I was trying wasn’t working, and assuming that it was all my fault as a clueless newbie. Turned out my code was perfectly good; the InstaREPL was mis-interpreting it. It’s still my fault, mind you: there’s something about the InstaREPL’s behavior that I’m not understanding that’s leading me astray. But after a couple of wasted programming sessions, I decided I needed to find something more traditional. (Let me be clear: Light Table is an interesting tool, and it’s going to remain on my radar for the time being. Your mileage may vary.)
To Be Continued! Why? Because it interests me, even if it interests no one else!