Sunday, September 16, 2012

Is Clojure a practical language / worth learning?

Over the past few days I have been teaching myself the very basics of Clojure (with a bit of difficulty), and I have been questioning whether or not Clojure is worth the trouble of learning about immutability, concurrency, and functional programming. After all, Clojure is one of the youngest and least cared about programming languages around, so why should the trouble be worth the time?
In this blog post I discuss whether or not Clojure is worth learning in my first installation on a line of blog posts concerning Clojure and functional programming.

Clojure is hard
The title says it all. As a programmer that has mostly worked in languages within the Algol branch (C++, Java, etc) the functional paradigm seems like a scary thing. Not being able to change the state of a vector or a map is equally as frightening at first, and having threads be such an integral part of the language does not soften the situation. Learning Clojure, especially since it is such a young language, is no easy task. Having a background in Python (or Lisp) and Java is almost an absolute must before you can even consider Clojure.

As you will read this hard work pays off.

Why Clojure is a good idea
A while ago I had a foray into Lisp and really left Lisp half-learned before I got antsy. The most recent documentation regarding Lisp was from around 2002, it had no unified (or good, for that matter) graphical toolkit, and it had major gaps in its usability in terms of strange forms (loop) and stack overhead when you don't want to use them. Although Lisp has its flaws, it is a wonderful language that allows the programmer to be concise with their code through something called Higher Order Functions. A single function can serve multiple purposes, a trait that I call programmatic nonlocality. In fact, I was able to rewrite an old text library from Java (originally 320-some lines) in less than 100 using Lisp.

However, Lisp has the shortcomings and the dead community that I described above. Meanwhile, the C*, Vala, Python, and especially Java communities (since the dawn of Android) have been experiencing a golden age of popularity. Not a single student in my High School Java class had written in Lisp; only one had so much as heard of its existence. Clojure is Lisp that compiles to the Java Virtual Machine, as you probably know if you are reading this article. That gives it the power to be compatible backwards and forwards with Java. This is called interoperability. Now you can have the elegance of Lisp without the guilt of using a nonpractical language. All of the Java libraries become available to you, and any Clojure libraries become available in Java. This comes into great use when dealing with functions conditionally has caused a massive decision tree, or when working with data trees requires good recursive capabilities. The "Hard Part" can be written in Clojure, and then you leave the sloppy imperative stuff up to Java.

The ability to enhance your Java programs with this "secret sauce" of computing is a trait that you should definitely look for as a programmer that is always looking to improve themselves. I have also found that the principles of Clojure are transferable to other programming languages. A programmer will think of how to solve a problem in Clojure and then implement it in whatever language they choose.

Downfalls
Most of the issues with Clojure will probably be short lived. In the world of programming Clojure is one of the youngest languages out there. (Right up there with Vala and LOLcode). The documentation is outdated, and bugs still exist within the language. Also, Clojure is not used on many projects yet because of its immaturity. This means that you may be the only person on your team that knows Clojure. Of course, that also implies that when you need help there will be a very limited community to lend you a hand.

Predictions
In general, most of the problems that Clojure faces comes from a small community of developers lucky enough to hear about Clojure before it gets big. I am convinced that Clojure will become more-and-more relevant in the coming years. Clojure is a language designed specifically to solve the problems introduced by other languages, and to create a modern toolkit for modern applications such as parallelism.

Applications
Clojure can be used wherever the 9 billion devices that use Java are used, even for Android applications. It compiles to a .class file that can interop with Java seamlessly.

Conclusion
Clojure is a strange language born out of a strange paradigm and the striving for perfection; the repair of the shortcomings of the languages before it. Learning Clojure will increase your ability as a programmer in general and how you use Java specifically to create efficient applications. Clojure is definitely worth learning, despite how hard it is for beginners and intermediate programmers alike.

Sunday, September 9, 2012

A quick review of Clojure

I just want to start off by saying that I do NOT KNOW CLOJURE. I have only experimented a little bit with it and do not have any professional opinions about it. These are my thoughts as a newcomer to the language coming from a Java / lisp background.

A bit about Clojure
Clojure is a Lisp dialect that compiles directly to bytecode for the Java Virtual Machine (JVM) like Java itself does. This gives it a property called Java Interopibility. This is one of Clojure's selling points. Java and Clojure are both completely compatible with one another in a compiled format. This means that you can use Java libraries in your Clojure project and vice versa. Even Swing / Awt graphical user interfaces can be integrated with your clojure project just as they would be in Java.

The Good
To start off Clojure IS a Lisp, and Lisp's syntax is terse and beautiful </opinion>. Clojure has fixed some of Lisp's problems, especially the awful way for handling iteration. In Clojure there is a macro called loop just like in Lisp, but the syntax is easier to follow. Here is an example using loop.
(loop [x 5] (print (concat "looping " (str x) " more times")) (if (= 0 x) nil (recur (- x 1))))
This is obviously written in terrible form since it is pretty procedural, but this is supposed to showcase how loop works. It is a let form that can be returned to with recur. The magic here is that this does not create a stack overhead. No more stack overflows! Of course, infinite recursion can become a problem. But now recursing can be much lighter on the resources and you no longer need to declare a local function to recurse.

The best thing about Clojure is the Java interop, though. Java is one of the most widely used programming languages out there, running on billions of devices and being responsible for Android, desktop applications, and embedded systems in some cases. When you use Clojure you do not lose this massive support, or any Java libraries you have coded up in the past.

Clojure is an open-source project and you can view the source code for all of the base Clojure functions right on the github page.

The not-so-good
Clojure has a confusing means of editing data directly since just about everything but variables (declared with (def)) are immutable; they cannot be changed. This pretty much forces the functional paradigm down the programmer's throat and can actually halt good practice in some cases.

The language is young and not very well known. Its popularity is on the incline into the mainstream, but for now it is still an obscure language. Very few practical programs are written in Clojure and the documentation leaves a bit to be desired on the official Clojure website.

Also due to its lack of popularity as of right now there are few Clojure libraries to play with. However, Java Interop makes this less of a problem than it would with other languages (like Common Lisp, for instance).

The tools used to write Clojure are not as good as Common Lisp's yet, in my opinion at least. Emacs does not have a Clojure-mode, but one can be added through a third-party repository (although I could not get it working for the life of me on Emacs 24). Eclipse also has a plugin called counterclockwise that hooks up to the Lein REPL and provides tools for saving and compiling Clojure.

Clojure Resources
#clojure on Freenode IRC seems like _THE_ meeting place to discuss clojure and ask for help, or just to brag about something you wrote in Clojure. Even Clojure Programming by Emerick Carper & Grand , the de-facto Clojure book for beginners and intermediate Clojure programmers mentions #clojure.

Saturday, September 1, 2012

Common Lisp Tutorial preamble

Welcome to my Lisp tutorial. Just a while ago Lisp was a foreign language to me, and through study and determination (and a boatload of help from IRC users) I am finally started to get a grasp on the language. As a return to the Lisp community I will be writing the most extensive guide on this blog yet in a hope to teach Common Lisp to a complete newcomer to the language.

On IRC, many were in favor of using Common Lisp as a first programming language. I am in strong support of teaching FUNCTIONAL  programming as the first paradigm, but I am not in support of using Lisp for this purpose. For this reason the tutorial assumes a background in another programming language, but what that language is does not matter. If you know what the terms global variable, function (method), and structure mean then you are ready to learn Lisp.