Getting Started with Clojure Development I: The REPL

This is the first post of one my ongoing attempt to write a series of blog posts about how to get started with development in Clojure. This post will cover a beginner's first encounter with the Clojure Read Eval Print Loop.

This post assumes a UNIX-like environment. Some details might vary from OS to OS (especially for Windows). In those cases, study the documentation related to your platform for the mentioned applications.

So, you've heard some interesting things about this language called Clojure... Good. When you play around with an interactive programming language (which Clojure is a prime example of) you usually do it though a shell of some sort. In Lisp languages, this shell is traditionally called the REPL, which stands for Read Eval Print Loop.

The question I can almost hear you think is: "How do I install Clojure?" The answer to that question, which is somewhat unusual, is: "You don't." Clojure's approach to language and library versions is similar to Virtualenv of Python or RVM of Ruby. To launch Clojure you use what's usually called a build tool. Since they do more than just build tasks, so you can think of them as "Clojure environment tools". Two of the most common ones are Leiningen and Cake. Here, I will only cover Leiningen, but Cake works nearly identically for basic tasks.

Now, what you do install is Leiningen:

With a bare REPL in a terminal you can do much, but the lack of editing and saving abilities can make it tiresome in the long run. I therefore recommend to use an ordinary text editor to write the code and then send the code to the REPL. The simplest way to accomplish this is to simply copy and paste the code, but more sophisticated editors provide more convenient methods (I will shortly show this can done in Emacs).

Restricting oneself to only the REPL has some serious limitations and because of this I don't recommend this approach in general, except for learning (for that it may indeed be very useful) and trivial projects. So far I haven't mentioned how divide code into multiple files, how to use third party libraries or how to specify the version of Clojure to use. For that, you need to set up a Leiningen project. This is the topic for the next (upcoming) part of this blog post series.

In Emacs, you can interact with the REPL by following these steps:

This is it for now. Happy hacking!

;; raek

  1. You might need to restart the X server for this change to be effective.


Comments are hosted on the same server as this blog. The server resides in the EU and no data is shared with third parties.