An introduction to Clojure

11 10 2009

I’ve started solving Project Euler problems to learn Clojure, a new language on the Java Virtual Machine

Before going into the problem solutions, i’ll give a brief introduction to Clojure

Clojure (pronounced like closure) is a dialect of the Lisp programming language. It is a general-purpose language supporting interactive development. (from Wikipedia)

The Lisp programming is one of the oldest programming languages still in use today, in fact it’s the second oldest, beaten only by Fortran.

One of the defining properties of Lisp is that its homoiconic, that is, the code of Lisp is written in lists.

Clojure is also a Lisp and has similar syntax. One of the properties of Clojure and other lisps is the interactive nature. Instead of a Code -> Compile -> Run cycle, there is a Code -> Evaluate cycle. Pieces of the code can be dynamically added to the running program image.

When you start Clojure, you will be handed a prompt which looks like this

>

Similar to a prompt, this is a REPL (Read Eval Print Loop) . You can type Clojure code here and immideately define functions and evaluate expressions.

Lets evaluate some basic expressions

> 5
5

> "Hello"
"Hello"

Whenever you type anything into the REPL it is code, what gets displayed is Clojure’s representation of that code as data. So in the case of typing 5, Clojure evaluates it as 5.

Similarly with “Hello” which is a String, Clojure create a String with a value “Hello”. Clojure can do floating point arithmetic just as easily so typing 5.5 wil give you 5.5.

Now onto an expression.

> (+ 2 3)
5

Clojure and other Lisps use the prefix notation, that is the operator first followed by the remaining operands. This could get some getting used to but stick to it and you’ll be prefixing in no time. This is a simple expression, the function ‘+’ followed by 2 and 3. This evaluates to 5.

One of the advantages of prefix notation is the ability to have arbitrary lists of operands so (+ 2 3 4) is just as easily understandable.

> (def a 5)
#'user/a

> a
5

> (* 2 a)
10

def associates a symbol with a value, as shown above a is defined with a value of 5. Now for all purposes a is 5. You can even asign an expression to a like (+ 2 3), this will be evaluated and the value assigned to a.

Don’t worry about the #’user/a, this just means that the symbol a has now been defined. the ‘user’ refers to the current namespace. You can have multiple namespaces with the same symbol associated with different values.

> (defn add [x y]
	(+ x y))
#'user/add

> (add 10 12)
22

defn defines a function the square brackets [] are the argument list to the function, in this case the function add takes 2 arguments x and y. The sum of the 2 arguments is returned as the result.

This should give you a basic idea about Clojure and enough to be able to understand the project euler solutions in the next couple of posts.

Advertisements

Actions

Information

2 responses

15 10 2009
Andi

Currently I’m learning Clojure too, and I really appreciate your articles 🙂 Keep on your good work!

15 10 2009
Sidhant Godiwala

Thanks and will do 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: