technology, philosophy, fiction

Clojure - the easiest functional programming language to learn

March 23, 2020

I am a fan of functional programming. As a computer science student in college, I read articles by legendary tech entrepreneur and venture capitalist Paul Graham (of Y combinator fame) praising the lisp programming languages, and this excellent article at defmacro on functional programming (although it seems slanted to Haskell). Although most CS students learn about various programming paradigms (object oriented, logic, functional, etc.) in school, it was only relatively recently that the functional paradigm started gaining traction in the ‘mainstream’ of corporate programming. I’m not going to make the case for functional programming here, because the aforementioned articles do it much better than I ever could (you should read them).

I have tried to learn functional programming before, using languages like Haskell, Scala, and F#, even going so far as doing a Haskell presentation during my programming language theory class in college. However I ended up quitting halfway through learning these languages. I struggled with the type system, or the tooling, or the language itself was just too big to wrap my head around. I had heard of Clojure and thought it sounded cool, but it wasn’t until 2019 that I really decided, on a break during grad school, to sit down and actually learn it.

Why Clojure?

Clojure has the advantage of being a language that is both easy to learn and also powerful enough to write production-level code in. You can think of Clojure as the ‘Python’ of functional languages. I recently took a graduate class on ‘Symbolic Programming’ which focused on the Common Lisp programming language. There are numerous differences between the two lisp dialects, but one that is immediately apparent is that Clojure is a much more ‘purely functional’ language. Common Lisp provides facilities for looping with the ‘loop’ macro, CLOS for object orientation, setf/setq for local variables. Common lisp is a multi-paradigm language that makes it much easier to fall back on imperative styles of programming. So if you want to ‘force’ yourself to learn FP, Clojure is a better option there. Another way Clojure stands out is that it has standard facilities for networking related functionality, whereas with Common lisp these seem implementation-specific. Finally, the documentation and community for Clojure seem more complete and helpful.

The two books I used to learn Clojure were ‘Clojure for the Brave and True: Learn the Ultimate Language and Become a Better Programmer’ by Daniel Higginbotham and ‘Web Development with Clojure, Second Edition: Build Bulletproof Web Apps with Less Code’ by Dmitri Sotnikov (there is a 3rd edition out now that is more updated). I am currently reading through a Clojure book on macros.

Sotnikov’s second edition of the book was somewhat dated in parts. I haven’t read it, but I would probably direct people to the third edition of the book, which is, as of the time of writing this, still in beta. With regards to Higginbotham’s book, it leans heavily on the Emacs text editor. If you are not familiar with Emacs, I highly recommend not using it since it will be a pain to learn Emacs and a new language at the same time. Using Visual Studio Code with various Clojure extensions like ‘Calva’ is almost as good and far more beginner-friendly. If you end up going this route you can skip the first bit of Higginbotham’s book which is mostly about Emacs.

I like learning by making real projects, not just exercises or bootcamp project faux-ecommerce apps. I ended up learning about Clojure by writing a free and open source imageboard (a type of anonymous internet forum). The whole project was written from scratch, not using a framework like luminus, but by adding individual libraries to the project.clj. I still update it from time to time as a side/passion project. If you have been struggling to learn FP, I highly recommend Clojure. Even if you don’t use it day to day, having learned FP using it will influence the way you write other languages. It’s an excellent language for hobbies and side projects. You can even write it on the front-end with ‘Clojurescript’, a version of Clojure that works in the browser (on a javascript platform). Clojure is a modern, performant, high level language with a solid ecosystem of libraries that don’t break. Give it a try!