Clojure, day one

So the second to last language in Tate's Seven Languages in Seven Weeks is Clojure. A lisp dialect hosted on the JVM. Created by Rich Hickey, Clojure aims to present a modern pragmatic lisp with a strong concurrency story (and a more eye friendly approach to parens :).

My previous experience with lisp comes in the form of Scheme which was the language used in my university AI class. I don't remember much , but I do remember liking the smallness and uniformity of Scheme. Unlike Scheme, Clojure is a much more fully fledged/batteries included language, and I have been looking forward to experimenting with this language, particularly since it is hosted on the JVM (there is a lot to be said for access and interoperability with that ecosystem). That said the exercises for this chapter were really really short. The chapter provides a quick introduction do basic clojure data structures and defining functions. As well as an all-too brief peek at the loop-recur form for tail recursion.

Here is my code the exercise.

As I said pretty straightforward (and very brief). There isn't that much to say about it (it really was just these two questions - i did a second version of collection-type just because i figured there must be an alternative to many nested ifs).

One thing that didn't come through all that clearly for me in the text was the syntax for loop-recur. Clojure does not have local mutable variables, thus traditional iteration tools like for and while loops are not available. The only iteration method provided is recursion. That said, clojure does not support tail call optimization (due to vagaries of targeting the JVM), so how can we do large iterations without blowing the stack? Clojure provides a form called loop-recur that allows one to re-bind variable on each iteration and does not add to the stack (its more like a goto). To better grok the syntax, i tried to implement a tail recursive fibonacci, and basically just copied over the code I wrote for Io day one. Here is the result.

On a complete tangent, I am also going to link to a video about a recent interesting development in Clojure land called ClojureScript!, a javascript target for the clojure language, designed to be used to write client side portion of browser-based apps. I haven't quite watched the video yet, I am in fact I am watching/listening to it now!.

Posted On 26th July 2011

comments powered by Disqus