Fork me on Github

Fork me on GitHub

Jun 23, 2012

Starting with Clojure

This post shares some tips and tricks for getting going with Clojure on a mac. First step is to install clojure from here. To get a nice a not too fast-paced introduction to the language I read the Clojure chapter in the great book Seven Languages in Seven Weeks.

Then you will want to install Leiningen, which is a building tool for Clojure that also provides a REPL.

There was a small trick required first, and that is to add the place where you put the Leiningen file to the $PATH.
I put the lein file into ~/bin and added this folder to the path by creating a .bash_profile file in my home directory with the command
vim .bash_profile

in the file I added
export PATH="$HOME/bin:$PATH"

restarted the terminal window, and saw that it worked by
echo $PATH

you launch it by using the command
lein repl


A small thing that I had to look up is how to add some set functions (not part of the core Clojure libraries)
The command is:
(require '[clojure.set])


Another gotcha is the difference between symbols and keywords.
Keywords are like erlang atoms and symbols are referring to something else than itself, like say a let-binding. All keywords are prefixed with a colon in Clojure, like so 
:car


A handy way to find out more about functions, is the doc feature, like so:
(doc str)


Anonymous functions in Clojure are defined with the command:
(fn [parameters] (function))


An abbreviation for the fn command is the hash symbol #, and this is what is used for declaring lambda expressions.