Introducing Clojure

Clojure · v1.0.2

2026-11-14 14:38:24

Where we are

Where this fits

  • The λ-calculus module built truth values, pairs, numbers and recursion from functions alone
  • Every construction there is a definition, an application, or a reduction
  • This module turns to a programming language descended from that idea: Clojure
  • Background assumed: a mainstream imperative language — Java, Python, C#, Ruby

Outline of key topics

  • The λ calculus as a programming language: what it does well and what it lacks
  • What the Lisp family takes from it: homoiconicity, macros, functional programming
  • Clojure: a functional Lisp, hosted on the JVM
  • First-class functions, persistent data structures, values and identities
  • Prefix notation and the two uses of parentheses
  • The JVM underneath: Java interop and threads

The λ calculus as a programming language

What the λ calculus does well

  • Simple syntax: three productions — a name, a function, an application
  • Composability: cond from make_pair, booleans from cond, arithmetic from numbers
  • Nothing is discarded: every definition is reused by the later ones

Composability: any program can be built by combining simpler programs.

What the λ calculus lacks

  • No native data: a number is a nest of pairs, a truth value a selector
  • Awkward repetition: a definition cannot name itself; recursion needs a combinator
  • No performance model: pred and add take time proportional to the value
  • No code organisation: no modules, namespaces or libraries
  • No interoperability: no input, output, or call to any other system

The λ calculus is a model of computation, not a practical programming language.

Example: the number three

three = λs.((s false)
           λs.((s false)
              λs.((s false) λx.x)))
  • three is three nested pairs; 1000 would be a thousand
  • (add x y) applies succ and pred once for each unit of y
  • reading the value back requires counting pred steps down to zero

A programming language needs data that the machine represents directly.

What Lisp takes from the λ calculus

A minimal, universal syntax

  • Simple: one form of expression, a parenthesised application
  • Universal: the λ calculus is Turing-complete
    • it was shown equivalent to the Turing machine in 1936
    • any computable function can be written in it
  • A language with this core can express every computation

Definition — homoiconicity

(def code '(+ 1 2))

(list? code)    ;; true
(first code)    ;; +
(count code)    ;; 3
(eval code)     ;; 3
  • A language is homoiconic when its programs are its own data structures
  • (+ 1 2) is a list of three elements: a symbol and two numbers
  • Known as code is data

Programs that construct programs are called macros.

The pure functional paradigm

What it gives

  • data structures are read-only
  • no state can change
  • a function’s result depends only on its arguments: safe to share and to test

What it costs

  • every “change” builds a new structure
  • copying costs time and memory
  • some problems are modelled more directly with mutable state

The Lisp family

Lisp: list processing

  • Designed in 1958 by John McCarthy; the second-oldest family in use, after Fortran
  • A family of languages, not one: Common Lisp, Scheme, Emacs Lisp, Clojure
  • Lisp introduced features now common elsewhere:
    • conditionals, automatic garbage collection
    • macros, functions as values

Two branches of the family

Functional-first

  • Scheme, Racket, Clojure
  • recursion and higher-order functions as the normal style
  • Clojure: immutable data by default

Multi-paradigm

  • Common Lisp: loops, assignment, an object system
  • Emacs Lisp: an editor’s mutable state
  • keep the syntax and the macros, not the functional style

The family is defined by its syntax and its macros, not by purity.

Clojure: a practical λ calculus

Clojure: a functional Lisp

What the λ calculus lacks What Clojure provides
native data numbers, strings, vectors, maps as literals
practical repetition functions that call themselves by name
a performance model JVM bytecode; persistent data structures
code organisation namespaces, and the JVM classpath
interoperability direct calls into Java libraries

Clojure keeps the λ calculus’s core and supplies what a working language needs.

Three pillars

Clojure is a simple and succinct language designed to use legacy code and multicore processors.

  • Lisp: a sparse, regular syntax, and metaprogramming
  • Functional programming: functions as values, immutable data
  • The JVM: existing Java libraries, and native threads

The three cannot be separated; each explains the others.

What makes a language functional

Definition — first-class functions

The minimum requirement to be a functional language is to treat functions as something more than named subroutines for executing blocks of code.

  • Functions are values, as "hello" and 42 are
  • They are passed as arguments, and returned as results
  • A language that treats functions as values has first-class functions

The three accompanying features

  • Pure functions with referential transparency
    • pure: no side effects — no global state changed, no I/O
    • referentially transparent: the same inputs always give the same output
  • Immutable data structures by default
    • guarantees a function cannot alter its arguments
  • Controlled, explicit changes to state

Immutability by default is what makes purity practical.

Clojure is pragmatic, not pure

  • Defaults encourage pure FP: immutable structures, higher-order functions, recursion
  • Some tasks are modelled more directly with mutable state and imperative code
  • Clojure provides well-defined constructs for sharing and changing state
  • It does not require side effects to be annotated
  • Java APIs are available directly, with their performance and their pitfalls

Persistent structures and structural sharing

Structural sharing

  • An immutable structure cannot change, so an “edit” must produce a new structure
  • Structural sharing: the new structure reuses every part of the old one it can
  • Only the minimum is copied; operations stay fast and conserve memory

The safety of passing by value, with the speed of passing by reference.

Example: adding e to a persistent tree

The tree xs: immutable nodes and references.

The new tree ys, sharing b, a, c and h with xs.
  • New nodes d', g', f' are created only on the path to the root
  • Everything off that path is shared between xs and ys

Values, identities, and controlled change

Value and identity

A value

  • cannot change
  • 42 is 42; subtracting 2 gives a new value, 40
  • true of every value, not only numbers

An identity

  • a container that holds a value
  • may hold different values at different times
  • a variable acting as the identity of something

Only identities change, and only by taking a new value.

What Clojure guarantees about change

  • One identity, atomically: threads see the value before or after, never during
  • Several identities, transactionally: software transactional memory (STM) commits all or rolls back
  • On another thread, without blocking

These are named here; the unit on state and concurrency treats them with code.

Prefix notation

Prefix and infix

(str "Hello, " "World!")
;; Result: "Hello, World!"

(+ 1 2)
;; Result: 3

(+ 1 2 3)
;; Result: 6
  • Prefix: the function comes first, the arguments follow
  • Infix: "a" + "b" + "c", the operator between operands
  • No operators: + is an ordinary function, so there is no precedence
  • Variable arity: another argument needs no extra operator

Nesting

(+ 3 (* 4 2))
(+ 3 8)
;; Result: 11
  • Infix 3 + 4 * 2 depends on precedence
  • Clojure requires the explicit form (+ 3 (* 4 2))
  • The inner form is evaluated first: (* 4 2) gives 8

Operators are more concise; prefix makes every call consistent.

What parentheses are for

Calling functions

Nested parentheses for calling functions.
  • The first form after ( is a function, macro, or special form
  • Every later form is an argument
  • Arguments may themselves be nested calls

A left parenthesis signals a function invocation.

Constructing lists

  • The second use: parentheses construct lists
  • A Clojure program is itself a series of lists, parsed, evaluated and compiled by the compiler
  • The same language features act at compile time and at run time
  • This is homoiconicity, and it is the basis of macros

Structural editors move whole expressions, so parentheses are never matched by hand.

The JVM underneath

Java is three things

  • A language, a virtual machine, and a standard library
  • Clojure compiles to JVM bytecode; it uses the library but not the language
  • Java types directly: strings are String, numerals are Long
  • Collections implement Java interfaces: Iterable, Collection, List, Map
  • No wrapper for abs, exp, sin, …: they are called on java.lang.Math

Clojure does not hide its host; programming in it requires the basics of Java.

Why hosted

  • A mature, ubiquitous VM: HotSpot’s JIT compiler and choice of garbage collectors
  • Its libraries: Joda Time, JDBC, Jetty, Bouncy Castle, Apache Commons
  • Its tools: VisualVM, YourKit, New Relic
  • Lower risk for an organisation already running Java
  • Clojure has also been hosted on the CLR and on JavaScript engines (ClojureScript)

Calling Java from Clojure

The dot form

(. Math PI)
;; Result: 3.141592653589793

(. Math abs -3)
;; Result: 3

(. "foo" toUpperCase)
;; Result: "FOO"
  • Read (. A B args...) as: in the scope of A, do B with the arguments
  • It is the doorway for consuming Java APIs

The sugared forms

Member Form Result
static field Math/PI 3.141592653589793
static method (Math/abs -3) 3
instance method (.toUpperCase "foo") "FOO"
constructor (new Integer "42"), (Integer. "42") 42
  • (Integer. "42") is rewritten during macro expansion to (new Integer "42")
  • The sugar changes how a call reads, not what it does

Threads and the concurrency story

JVM threads and Clojure’s constructs

  • A thread is a line of program execution; every program has at least one
  • JVM threads map to native threads, so programs use every CPU core
  • Shared mutable state across threads is hard to make correct in Java
  • In Clojure, immutable data removes most shared mutable state
  • Where state must change: vars, atoms, refs, agents
  • Parallel work without shared state: futures and promises

Summary

Summary

  • The λ calculus is simple and composable, but not a practical language
  • Lisp keeps its syntax: homoiconic, and so open to macros
  • Clojure is a functional Lisp hosted on the JVM
  • First-class functions are the minimum for a functional language
  • Structural sharing makes immutability affordable
  • Values never change; identities take new values
  • Prefix notation: no operators, variable arity; parentheses call and construct
  • The JVM supplies libraries, interop and native threads

Where next

Clojure Elements: Data Structures and Functions — the next unit in this module.

  • The REPL
  • Clojure’s core data structures
  • Program structure and program flow