Categories
clojure programming

The Newbie’s Guide to Learning Clojure

There are many ways to dabble in Clojure. My approach has been to read something, retain some fraction of it, and then repeat this process until those fractions equal a whole. When stepping into functional programming for the first time, especially after OOP, it’s not easy to grasp it right away. Sooner or later, the ideas will stick, right? That means learning in multiple passes, where some deeper level of understanding happens during each pass compared to the last. Kind of like iTunes Essentials with “The Basics”, “Next Steps”, and “Deep Cuts”. So that is what I want to present to you, in an ordering that will minimize your difficulty along the way.

Getting Off the Ground

It might be easiest to start with online tutorials, and the first one would be from The Blackstag Blog, “Guide to Programming in Clojure for Beginners”.

(10/26/2013: Actually, before starting with “Guide to Programming in Clojure for Beginners”, I recommend starting with Clojure from the ground up: welcome. Its Clojure environment instructions are up-to-date and slightly gentler, and its explanation of Clojure syntax and evaluation is quite gentle, too.)

While you’re following along with that, if setting up a Clojure environment is too hard, you can use the one offered at Try Clojure. There’s a limited tutorial you can run on top of that. That will hopefully get you in the practice of trying things out.

Following that, there’s “Clojure – Functional Programming for the JVM” by R. Mark Volkmann. This is a great, dense packing of the highlights of Clojure in one page. One long but useful page.

Getting the Hang of Things

The better some piece of technical writing is at explaining things, the easier its subject appears and the less tempted you might be to try it yourself. As different as Clojure is to the OOP world, we want to avoid that and really dig into it and get our hands dirty. Doing is the best way of learning, but it’s all the more necessary here. So we should do two things at the same time: practice what we know on progressively harder mini-challenges, and reading a proper book to get a proper grounding, so to speak.

Some great tests can be found via a great interface at 4Clojure. The tests start out with the type “do you know ___” and progress to harder things, where you have to draw on your knowledge of the language’s core functions and compose them in increasingly clever ways.

Others report success in learning-through-practice with the Clojure koans.

You also want to start reading a book so that you get exposed to a listing of core functions and more features of the core language. Perhaps the best books for learning the gist of Clojure right now are Programming Clojure and the soon-to-be-released Clojure Programming. The book I first read was Practical Clojure, but it is not the best book, and it is outdated now. Programming Clojure is comprehensive, but it requires you to read serially to get all the information together, as opposed to allowing random access without losing any benefits (if that’s possible). I haven’t yet read Clojure Programming.

(10/26/2013: Now that I have read Clojure Programming, I can say that it is fantastic book. It is both broad and deep in its explanation about Clojure — it has something to offer Clojure programmers at nearly every level of expertise (unless you wrote the language). Although I think it is a must-have on your bookshelf, my instinct is that it is something that I would leave until later. Personally, I read it after I read The Joy of Clojure (see below), and I think that is a good ordering so as not to scare off newcomers.)

The way I practiced Clojure was to use problems from Google Code Jam and wrote Clojure code to solve them, and drew on Stack Overflow a lot. But this was only after reading my book on Clojure basics, without which that probably would’ve been too big of an undertaking.

Once you get into practicing Clojure, you start to see what separates Clojure/Lisp from other programming languages: without even mentioning macros, we can say that Lisp is to programming what Unix/Linux is to command-line work. We have a basic abstraction (sequences/lists; streams) and we have a huge set of tools (functions; minimalist programs) that operate on that abstraction and can be combined and composed in a multitude of simple ways to accomplish a huge variety of outcomes. The result for Lisp programming is that the code you write no longer has incidental complexity — the code you write closely follows the contours of the problem you’re trying to solve. I’m not at the level of using macros, even if I’m slowly understanding what they do and how to use them, but I believe others when they speak of incredible power that is unique to Lisp.

As you start learning more about the language, you’ll want to keep a reference sheet handy to help you keep your knowledge organized. The simplest way to accomplish such organization is through the “official” Clojure cheatsheet. The cheatsheet organizes core functions by their logical themes. (The cheatsheet will be upgraded soon to a new version with tooltips.) The Clojure Atlas is an evolving tool that shows a network graph of functions based on how closely related to each other. People who have used it in their early learning report that it was worth the nominal, one-time payment to them. If you need examples of how to use the different functions (in clojure.core and user-contributed libs), ClojureDocs is your place to go. You may have already ended up here from previous Google searches. A nifty and elegant tool is the clj-ns-browser, a GUI that lets you browse namespaces (loaded/unloaded in your REPL) to find functions, documentation text. It has now pulled in the ability to fetch example code from ClojureDocs. The official Clojure API is more just a listing of all the main Clojure namespaces, and an alphabetical listing of the functions within each namespace.

Soaring High

You will want to keep practicing through programming. The more you do it, and the more you ask yourself (and subsequently, ask Google and Stack Overflow) how to do something (or how to do something in a better way), the more you continue on your trajectory of learning and getting better.

But then you will want to read The Joy of Clojure. As you start writing more complex programs, you will hopefully start to wonder if you’re “doing it right”. That’s what The Joy of Clojure will teach you. It contains all of the different features of the language not mentioned elsewhere, as well as Lisp-y ways of structuring your program.

(10/26/2013: I highly recommend, when you are ready, to read Clojure Programming end-to-end. It will take a while, across multiple sittings, but you will most certainly be enriched in many ways, even in ways you cannot predict. Part of the reason is that Clojure Programming gives practical, real-life examples of many of the features it describes (from the basic to the advanced). Another reason is that it dives down into the fundamentals of the language, and while those details might be “distracting” for beginners, but the book shows how those details are useful in simplifying more complex operations. I look forward to revisiting the “Practicum” chapters at the end of the book that show the Clojure version of doing web development using Ring, Compure, and Enlive. Clojure Programming is well-written and even has a philosophical style about it, which I certainly appreciate, and it fits with the depth and insights that the book reveals. Just don’t let it deter you, if you pick up Clojure Programming as a beginner, and it’s not ‘easy’ at first. You might have to put it down and come back to it (personally, I did), or you might have to think harder when you read through it; but after all, you think more when you write Clojure so that you can write less code that is simpler, and ultimately, ‘better’.)

While you’re doing that, you might want to view some of the presentations by Clojure’s developer, Rich Hickey, about Clojure and its different aspects. There are a few videos on InfoQ, a few videos on the Blip.tv Clojure page Clojure page/channel on Youtube, and then this 1-on-1 interview/demonstration video conducted by a long-time Lisp programmer . On the Clojure channel on Youtube, you have enough videos to reinforce whatever you might need that’s right for you: some videos are on the basics (ex: data structures part 1 & part 2, sequences, concurrency), some are geared for Java programmers (part 1, part 2), and some are for Lisp programmers (part 1, part 2). These videos aren’t necessary, but sometimes you get some interesting tidbits, like dynamic Swing GUI development and the famous ants demo. (The ants demo has 50 threads that interact with each other, only about 50-100 lines of actual concurrency/state-related code, is instantly scalable, and at 50 threads requires time delays to slow it down for humans.)

My discussion doesn’t feel complete without mentioning Paul Graham, and if you haven’t read his essays on Lisp, you should. (Some are easy to locate, while other important Lisp essays, like “Beating the Averages”, are in the Essays section.) He did a lot of Lisp work in Common Lisp, and the books he wrote are about or use CL. But it’s easy to translate that to Clojure . When it comes to learning macros, I think his treatment of macros in On Lisp is the best I’ve seen on macros, and worth reading before the alternatives, whether they are from a Clojure resource or not.

Once you get good and have time to spare, don’t forget to give back to the Clojure community, if you can. ClojureDocs has functions that could use any / some code examples. There is an ongoing migration of clojure-contrib 1.2.0 to be compatible with clojure 1.3 and its modular repository setup for contrib. The core of the Clojure language developers are pressing ahead with implementing logic programming in Clojure via core.logic. But there are plenty of things that us mere mortals might be able to work on to make a difference, such as creating/wrapping/porting useful functionality in Clojure. For example, plenty of cool Java libs live at Apache Commons, but only a fraction of that is standard in Clojure — should there be a Clojure-y way to achieve that functionality (and avoid Java or Java interop)? If you pay it forward, your Clojure world will become a better place.

(discuss)

4/7/2012: Added info on Clojure documentation sites and references


Thanks to Julio Barros and Siavosh Bahrami for taking a look at a draft of this post (and inspiring and pushing me to publish it).

18 replies on “The Newbie’s Guide to Learning Clojure”

Thanks for this.  Its a great step by step guide on how to start out and it has a lot of resources I didn’t know about.

You’ve written an awesome resource, which deserves to be known far and wide. If you haven’t already, consider posting its availability on the Clojure Google group.

I do disagree with your assessment of the various Clojure books, though. I have all of them, and each has its points. I found Programming Clojure (first edition, that is) to be poorly organized, and Practical Clojure to be–still–quite useful. My main takeaway to people reading this article: Your Mileage May Vary.

Awesome. I have been starting and stopping at various stages with Clojure – will start again with your recommendations. Thank you

I finally couldn’t get past the fact that clojure is just a butt-ugly language. I am sure that by 1965, LISP will take over the world with its unparalleled syntax and programming model. I can’t imagine a world where LISP doesn’t eventually become the predominant programming language. 

This is a good post because people need more guide posts.  I feel like the best way to learn clojure is to tackle the basics : parsing files, regular expressions, and writing sorting algorithms.  Things that we already know how to do.

Thanks for the post; I’m moving from Python/Ruby to Clojure, and this has some great ideas to get started.

The link to Clojure on blip.tv is dead, saying “404 – That Blip has sailed.”.

Thanks a lot, I almost want to quit my PHP job to learn this language. The few and small experiments (and Rich Hickey’s talks) makes me want to learn it for real. Thanks for the resource !

Thanks for the heads up! For some reason, Clojure videos had to migrate from Blip.tv to Youtube, and I updated the links accordingly as best as I could.

Haha, well, if you do quit your job, plan out your job first if you need to before making any moves! Personally, I had quit my job in order to work on my own project full-time, and I just happened to decide to learn and use Clojure to implement that project. In the absence of hand-holding beginners resources, it took 2 months to get familiar, but it was well worth the time. I want to revisit the chapters in Clojure Programming about web programming (Ring, Compojure, and Enlive) — very cool. On the front-end side of web programming, I know that there are clever ways to hook up a ClojureScript browser REPL to a Clojure REPL, but I need to learn more about it and become more familiar. Light Table (written in Clojure/ClojureScript) offers a nifty take on JS-HTML programming with watches : http://www.chris-granger.com/2013/08/22/light-table-050/

If there are parts of the Clojure learning experience, ecosystem, tooling, etc. that seem unclear or too difficult, let me know… Clojure as a language is all about simplicity, and we should be able to chart a course of learning Clojure so that learning it becomes as easy as the language is simple. 🙂

Typo ? In your edit “10/26/2013: Now that I have read Programming Clojure”, shouldn’t it be “Clojure Programming” ? It looks like so, the excerpt I read from “Clojure Programming” indicates this is the in-depth book you’re talking about.

Leave a comment