How to read Clojure forms and the literals you’ll see most: numbers, strings, keywords, symbols, and nil.
Clojure’s syntax is tiny on purpose. Once you can read a form and recognize the common literals, you can navigate real code quickly.
In Clojure, code is written as lists. The first element is the operator and the rest are arguments:
1(+ 1 2 3) ; => 6
2(str "a" "b") ; => "ab"
3(map inc [1 2]) ; => (2 3)
Two practical rules:
() usually means “call something.”[], {}, and #{} usually mean “data.”Unlike Java, there’s no “statement vs expression” split. Most forms evaluate to a value and can be nested.
142 ; integer (long-ish)
23.14 ; double
31/2 ; ratio (exact fraction)
410N ; bigint
52.5M ; bigdecimal
1"hello" ; java.lang.String
2\a ; character
3\newline ; named character
Only false and nil are falsey. Everything else is truthy (including 0, "", [], and {}).
:user/id is a keyword (commonly used as a map key). Keywords are also callable for map lookup:
1(:user/id {:user/id 42}) ; => 42
user/id is a symbol (a name that resolves to a var/function when evaluated).
1; line comment
2#_(+ 1 2) ; ignore next form (handy in the REPL)
let vs def (local vs global)Use let for local bindings (like Java locals):
1(let [x 1
2 y 2]
3 (+ x y))
4;; => 3
Use def for top-level vars (like Java static fields). As a rule of thumb: use def at the namespace boundary, not inside function bodies.