Introduction to Clojure
- 10 August, 2009 17:27
- Comments 4
Clojure is a Lisp-dialect which runs on the Java Virtual Machine (JVM). It is a dynamic programming language intended for general-purpose use. It also provides easy access to the Java standard library functions. In this introduction, we show you how to get started with Clojure.
Clojure is quite easy to install. The main requirement is a recent Java Virtual Machine (JVM) - 1.5 or later. You can download Clojure from the project download page at http://code.google.com/p/clojure/downloads/list. The latest released version is clojure_1.0.0.zip.
You can find a file called clojure-1.0.0.jar inside the zip file which contains the Clojure libraries. That file is all you need to get started. A key feature of Clojure is its shell-like interface, called the read-eval-print loop (or REPL), which you can start as follows:
java -cp clojure-1.0.0.jar clojure.lang.Repl
You will then get a "user=>" prompt which you can use to type commands for evaluation. For example, you can do basic arithmetic:
user=> (+ 1 2)
3
user=> (/ 8 2)
4
Note the order of the operands:
user=> (- 8 2)
6
user=> (- 2 8)
-6
You can also do nesting:
user=> (* (+ 2 2) 8)
32
Or use multiple arguments:
user=> (+ 1 2 3 4 5)
15
The examples above are written in a form known as a S-expression, which is
similar to all forms of LISP. The basic structure of an S-expression is:
(function arg1 arg2 ...)
There are several pre-built functions that you can use as building blocks.
For example you can check whether two values are equal using the "=" function:
user=> (= 1 2)
false
user=> (= 1 1)
true
Boolean arithmetic can be done through the ‘or’, ‘and’ and ‘not’ functions:
user=> (and true true)
true
user=> (and true false)
false
user=> (or true false)
true
user=> (or false false)
false
user=> (not true)
false
With these building blocks, you can now write your own functions:
user=>
(defn factorial [n]
(if (<= n 1)
1
(* n (factorial (- n 1)))
)
)
#'user/factorial
user=> (factorial 0)
1
user=> (factorial 1)
1
user=> (factorial 3)
6
You may have noticed there's a problem in the above function. Giving
it a negative number as an argument will cause a stack overflow. We can solve
it by using Exceptions:
user=>
(defn factorial [n]
(if (< n 0)
(throw (Exception. "Can't find the factorial of a negative number")))
(if (<= n 0)
1
(* n (factorial (dec n)))
)
)
#'user/factorial
user=> (factorial -3)
java.lang.Exception: Can't find the factorial of a negative number (NO_SOURCE_FILE:0)
A similar example is a function to find the nth number in the Fibonacci number series:
(defn fibonacci [n]
(if
(or (= n 1) (= n 2))
1
(+ (fibonacci (- n 1)) (fibonacci (- n 2)))
)
)
#'user/fibonacci
user=> (fibonacci 1)
1
user=> (fibonacci 2)
1
user=> (fibonacci 3)
2
user=> (fibonacci 4)
3
user=> (fibonacci 5)
5
What if we wanted to view the first 10 numbers? We can create a lambda function, by using the "map" and "fn" functions:
user=> (map (fn [x] (fibonacci x)) [1 2 3 5 6 7 8 9 10])
(1 1 2 5 8 13 21 34 55)
For those of you have been hanging out for a "Hello World" example: you can do that quite simply:
user=> (println "Hello, world")
Hello, world
nil
user=>
And one last example:
user=> (. System/out (println "Hello, world!"))
Hello, world!
nil
Since Clojure runs on the JVM, it's also possible to access the standard Java library from Clojure code. The example above is the Clojure equivalent of this Java code:
System.out.println("Hello, world!")
We've only covered a small fraction of Clojure, but it should be enough to get you started. To learn more, check out the Clojure getting started page (and the links in the reference section, as well as Clojure wikibook.
- Bookmark this page
- Share this article
- Got more on this story? Email Computerworld
- Follow Computerworld on twitter
- Sun Blade 6000 Modular System: Power and Cooling Efficiency
- Demonstrating Return on Investment with Enterprise-Class Identity and Access Management Technology
- Lost USB keys have 66% chance of malware
- Improving the Management and Sharing of Massive Data Volumes
- OVUM TECHNOLOGY AUDIT: HP Application Lifecycle Management
-
Sell-off angers Dick Smith
-
H-1B workers are better paid, more educated, study finds
-
Microsoft at a loss over Event Viewer scam
-
Samsung Galaxy Y Android phone (preview)
-
Seattle police say 'wardrivers' are hitting small businesses
-
Windows 7 for Dummies® Dvd+book Bundle
-
Computers for Seniors for Dummies, 2nd Edition
-
Office 2007 All-In-One Desk Reference for Dummies
-
Excel 2007 All-In-One Desk Reference for Dummies
-
MYOB Software for Dummies 6E Australian Edition
-
Microsoft Office
-
Windows 7 for Seniors for Dummies®
-
Office 2007 for Dummies
-
Teach Yourself Visually Windows 7








Comments
Anonymous
Great addition to the A-Z series! Thanks!
David
Question...
Instead
Question...
Instead of:
user=> (map (fn [x] (fibonacci x)) [1 2 3 5 6 7 8 9 10])
Can't you just do the following?
user=> (map fibonacci [1 2 3 5 6 7 8 9 10])
Anonymous
(range 1 11) will produce the sequence of integers from 1 to 10.
(println "hello world") is sufficient to print the string followed by a newline to whatever output stream is specified by the value of *out*. see (doc print)
Algorithms: A Functional Programming Approach by Rabhi and LaPalme is a good book to help get a better grasp of functional programming if you are getting started.
While straightforward to read, the fibonacci implementation is not very efficient.
Anonymous
Yes; I don't know why they did it that way...
Post new comment