What is Clojure?

In short
Clojure is a functional programming language that runs on the Java Virtual Machine. Its programs are written as data structures, such as lists, vectors and maps, which makes it easy for code to read and transform other code. This page explains the reader and the core syntax that every Clojure program starts from.
How Clojure source becomes data
Clojure is a homoiconic language. In plain terms, this means that Clojure programs are themselves made of Clojure data structures. The compiler does not work directly on raw characters. It works on the forms that the reader produces from the text.
Most Clojure programs begin as text files. The reader parses that text and returns the object each form represents. The reader is exposed as the function read, which takes the next form from a stream rather than the next character. Because of this design, the reader and Clojure's data literals are useful on their own, in many of the same places where you might use JSON or XML.
The official reference describes the reader as a set of syntax rules defined over characters, while the language itself is defined over symbols, lists, vectors, maps and sets.
The core reader forms
Clojure has a small set of literal forms that cover most data you will write:
- Lists are zero or more forms in parentheses, such as
(a b c). Calling a function is written as a list whose first element is the function. - Vectors use square brackets, such as
[1 2 3]. - Maps use braces with key and value pairs, such as
{:a 1 :b 2}. Commas count as whitespace, so{:a 1, :b 2}is the same map. - Sets use a hash followed by braces, such as
#{:a :b :c}. - Strings are written in double quotes and may span several lines.
- Keywords start with a colon, such as
:fred. They are commonly used as map keys.
Symbols are names. They begin with a non-numeric character and may contain letters, digits and a small set of punctuation such as *, +, !, -, _, ?, <, > and =. A single slash separates a namespace from a name, as in my-namespace/foo. A dot can appear in the middle of a symbol to name a fully qualified Java class, such as java.util.BitSet.
Numbers follow Java conventions. Integers are read as Longs when they fit and as BigInts otherwise. An N suffix always forces a BigInt. Ratios such as 22/7 are supported, and integers can be written in other bases, such as hexadecimal with the 0x prefix.
Macro characters and the read table
Some characters trigger special reader behavior instead of being read as plain text. These are called macro characters. Among the most common are:
- The quote character, which turns
'forminto(quote form). - The at sign, which turns
@forminto(deref form). - The caret, which attaches metadata to the next form. For example,
^:dynamic xattaches the metadata{:dynamic true}tox. - The hash-dispatch forms, such as
#"pattern"for regular expressions,#'xfor var quoting, and#_to skip the next form entirely. - The semicolon, which starts a comment that runs to the end of the line.
Anonymous function literals use #() with the arguments %, %n and %&. The documentation notes that this form is meant for short, one-off functions, and that #() forms cannot be nested. For anything longer, fn is the better choice.
Regular expression patterns are compiled when they are read. Backslashes inside #"..." are taken literally, so #"\s*\d+" is clearer than the equivalent string with doubled backslashes.
A small example
Because code and data share one format, you can inspect a form as plain data:
(def order {:id 42 :items ["book" "pen"]})
(get order :id)
;; => 42
(conj (:items order) "cup")
;; => ["book" "pen" "cup"]
The map and vector here are data, and the functions get and conj operate on them. Macros work the same way, since they receive forms as data and return new forms.
Common mistakes
Beginners often forget that a list in source code is a call. Writing (1 2 3) tries to call the number 1 as a function. Quote the list with '(1 2 3) if you only want the data.
Another frequent issue is mixing up symbols and keywords. The symbol name refers to a binding, while the keyword :name is a value you can use as a map key. Their behavior is different.
Finally, remember that the reader handles characters before the compiler runs. A stray character, such as an unbalanced bracket, stops the program from being read at all.
When Clojure fits
Clojure suits projects that benefit from immutable data and a compact syntax for transforming data. Because it runs on the Java Virtual Machine, it can call Java libraries directly. Teams that want to read and generate code programmatically often find the homoiconic design useful. If you are new to Lisp-style syntax, expect a short adjustment period before the parentheses feel natural.
FAQ
Is Clojure the same as Java?
No. Clojure runs on the Java Virtual Machine and can call Java classes, but its syntax and core data types are different. Java code is not required to use Clojure.
What does homoiconic mean?
It means a program is written in the same data structures the language uses for its own data. Clojure code is made of lists, vectors and maps, so programs can read and transform other programs.
Why does Clojure use so many parentheses?
Lists are written in parentheses, and function calls are lists. The syntax is uniform, so the same structure works for code and data, which keeps the reader simple.
What is the difference between a symbol and a keyword?
A symbol names a value or binding, such as a variable. A keyword starts with a colon, such as :name, and is often used as a map key because it evaluates to itself.
Sources
This page is based on the official documentation below. Read it for full detail.
Clojure — official documentation ↗