Thursday, March 8, 2012

Learning Clojure and the art of functional programming...

  This post describes an overview of my experience with the Clojure programming language, why I learned it, how I learned it, what it's like, and where am I going with it.

Over the past few months, I have been programming a bit in Clojure.  A direct descendant of McCarthy's eternal Lisp, Clojure is a functional language that runs on the JVM, fully interlopes with Java, and has excellent concurrency support with its Software Transactional Memory.   Clojure trades all the boilerplate code of brackets and semicolons common to  a language like C or Java, for function calls, parens, and macros.  The level of expressiveness in code is truly helpful when hashing out a complex idea or algorithm.  For example, I implemented a Burrows-Wheeler transformation https://gist.github.com/2004382 in under 40 lines.

I became comfortable with Clojure by taking a problem solving approach to learn the language, with the help of the website 4Clojure.org.  4Clojure presents a programming challenge, for example implementing library functions like  'map', that requires the user to write a function to satisfies a set of unit tests.  By working the problems in increasing order of difficulty, the concepts of functional programming, IMO the hardest part to learning lisp, came naturally.  For a period I became active on freenode's #clojure IRC channel, where I meet some fellows who I now share Clojure advice and code with via email chain and google group, http://groups.google.com/group/clojure-hackers?hl=en  As for Clojure literature, I find The Joy of Clojure most helpful for technical questions and best practices. For theory, Functional Programming Practice and Theory by MacLennan really helped me grasp the concepts of on a more abstract, mathematical rigorous level.  If you want to understand the logic and correctness of FP, this book is it!  Finally, Pearls of Functional Algorithm Design contains a wealth of algorithms collected from mostly academic journals and other sources implemented in Haskell and easily converted to Clojure.

Clojure, as a young language, (2007) lacks the development tools and packages of a more mature production language although using Java classes and libraries solves the problem of a small code base.  The source code I have read is well documented(clojuredocs.org), and between IRC and stackoverflow.com there are enough participants to field most questions.  As for an editor, I have found VimClojure very suitable for syntax highlighting, making it easy to write functions and sample data to a file that is then loaded into an adjacent REPL and evaluated.  For the community, I imagine Clojure now to be a lot like Ruby before Rails came out,  the people involved are highly motivated, and language shows great promise.  Fortunately for me, Clojure already does everything I need, even without a killer app.

In conclusion, Clojure has opened my eyes to a completely new way of thinking about a program: as the art of manipulating program state though time, transforming the logical identity of the input into that of the desired output (more here, http://clojure.org/state ) using the most powerful tool and rudimentary tool, the function.  The practice of learning a new language in a different paradigm has helped me challenge the conventions and assumptions of procedural coding,  opening the door for me to learn R programming on a much deeper level. When it comes down to choosing the next language for a future project, Clojure's ability to bring functional programming to the JVM/Java is very enticing, offering a very easy way to express my ideas as programs in a way no other language can.