Clojure: Lisp on the JVM

I have found a new toy! It is called Clojure. What is it? It is a Lisp dialect that runs on the JVM. Hey, hey, where are you going!

Crap! Around 85% of the readers just left the site in disgust. Some of them have probably un-subscribed from my feed as soon as they saw the word lisp. Oh well, well have to finish this without them.

What’s wrong with Common Lisp you say? Nothing wrong with it. But if you take a language and put it on top of JVM you now have double the amount of power. Here is the thing about programming languages. No matter which one you pick, it will be missing some library or functionality you will need for your project. It’s always a different thing that is missing, but you will never get a language that has everything. And that’s ok. The JVM languages however have the advantage of being able to tap into the incredibly rich Java API, or to seamlessly invoke stuff written in other JVM languages such as Java, Jython, Javascript (with Rhino) and etc..

This gives you a great deal of power and flexibility as you can imagine. This is why I love Rhino which allows me to produce Java compatible code without actually using the Java syntax. Clojure will allow me to do the same thing but with Lisp.

Of course there other JVM implementations of lisp such as Armed Bear Common Lisp (ABCL) or Second Interpreter for Scheme Code (SISC). All of these are fine, but I like Clojure because it seems to be a very progressive project. Instead of simply porting CLISP or Scheme to JVM they decided to create their own flavor, one built from the ground up with Concurrency and Java interop in mind. Traditional lisps don’t do concurrent programming. Since JVM has a great theading support so some of their implementations (like SISC) try to graft it in. But they are bound by backward compatibility and other concerns so the results may vary.

Clojure on the other threads it’s own path, abandoning some traditional conventions and implementing modern ideas. For one it is strongly concurrent, providing interesting abstractions for dealing with synchronous and asynchronous data access problems. Correct me if I’m wrong but I do not thing that any other lisp implementation has such a through and well thought out threading support.

But that’s not all. Clojure natively implements modern data structures such as hash maps and vectors making them behave as functions. Let me show you and example:

(def a (hash-map "foo" "bar" "biz" "bif"))
(a "foo") ; returns 'bar'

The language also extends standard lispy sequence functions such as map and filter to all objects that implement the Java iterable interface. So you can easily apply them to all the crazy custom data structures from your legacy code base. It also supports attaching metadata to your objects. It allows you to associate arbitrary map (eg. hash-map) to an object which does not affect that objects hash for comparison purposes, and doesn’t count as part of that object but which can be accessed at any time from within your code.

There is more stuff there but you can read about it on the Closure homepage. The point is that this feels like a new, vibrant language that is going places. When you work with CLISP or Scheme you are dealing with a mature, and pretty much static language. They are pretty much set in stone at this point, and they are very reluctant of introducing new features.

Clojure on the other hand is in active development, and is constantly being improved. It has a thriving community, a well maintained wiki page and a clearly defined set of goals and philosophy. It is a very exciting project – and one of the very few times, that someone is doing some new and exciting things with Lisp. It is a good idea to keep an eye on it. I’m not saying it will become the next big language. I mean, come on – it’s lisp. But it does take lisp into new and interesting directions which is a good thing and for one I’m excited about it.

If you want to start messing around with it, you will need to do three things:

  1. First download the clojure.jar and put it somewhere
  2. Second download jline.jar and put it in the same directory (if you skip this part you won’t have history, and left/right arrow support in the console interpreter)
  3. Make put this bash script somewhere in your path:
#!/bin/bash 
CLOJURE_DIR=/path/to/clojure
CLOJURE_JAR=$CLOJURE_DIR/clojure.jar
 if [ -z "$1" ]; then 
   java -cp $CLOJURE_DIR/jline.jar:$CLOJURE_JAR jline.ConsoleRunner clojure.lang.Repl    
else
   java -cp $CLOJURE_JAR clojure.lang.Script $1
fi

This will allow you to invoke the command line interpreter by calling your script without any arguments, or run a script provided on the command line. That’s all you really need to get started. Or you can just run it from the command line but as with many of these JVM language things these invocations tend to get way to long to type them in by hand each time.

So if you are a Lisp person, or you are interested in learning some lisp go and check it out. You don’t need to install anything. Just download few files, put them in a temporary diectory and start plugging away. If you don’t like it, delete them and your done. How easy is that?

This entry was posted in programming and tagged , . Bookmark the permalink.



4 Responses to Clojure: Lisp on the JVM

  1. astine UNITED STATES Google Chrome Windows says:

    Yes Clojure is an awesome project. Making vectors and maps first class and recursable is a massive improvement over the old common lisp onions. It’s funny that after almost 20 years of near stagnation in common lisp it would take a new dialect, one that was separate from the control of the ANSI standard and entrenched businesses, to actually kick life back into the language. (Everyone was hoping it would be Arc.)

    Of course, I personally hate messing with the JVM (classpaths) So I’m still in common lisp for now. The thing with lisp is, that there is no feature in any language that you can’t add to lisp as well, on the fly, and make it feel native. So until, I need the sort of concurrency that you can only get with ML/Haskell style functional programming, I’ll be happy where I am.

    Reply  |  Quote
  2. Tom Emerson UNITED STATES Mozilla Firefox Windows says:

    For me dealing with classpaths in the Clojure world is no worse than dealing with ASDF in the Common Lisp world. They’re obviously not entirely comparable, but each gives us some pain. One of the Big Wins for Clojure, for me, is the ability to interoperate with the large number of third-party Java libraries in addition to the Java libraries.

    Reply  |  Quote
  3. astine UNITED STATES Google Chrome Windows says:

    That would be Clojure’s big win, unless you’re looking for html generators or test frameworks, libraries in common lisp are rather scant. Though, I will tell you, ASDF is a lot easier on Unix, especially if you’re using clbuild.

    Reply  |  Quote
  4. Luke Maciak UNITED STATES Mozilla Firefox Windows Terminalist says:

    @astine: I agree with Tom. Java classpaths are not so bad. You set them once, make a script and then forget about them. :)

    VM’s are a good thing. We have two big ones right now that are likely to be installed on just about every computer out there – JVM and .NET. But a lot of people do not view JVM as a language agnostic platform (which it is). They see JVM and think Java.

    What Microsoft did right, was to separate their VM from their languages. Sun ought to really move in the same direction. Invest more in promoting JVM as a platform. It’s happening anyway, whether they want it or not.

    There are so many really attractive languages that run on JVM now – Rhino, Jython, Scala and now Clojure just to name a few. :)

    Reply  |  Quote

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>