Skip to content

Instantly share code, notes, and snippets.

@finalfantasia
Last active June 3, 2017 01:41
Show Gist options
  • Save finalfantasia/98e0d33110c0d777396bd61defb177a8 to your computer and use it in GitHub Desktop.
Save finalfantasia/98e0d33110c0d777396bd61defb177a8 to your computer and use it in GitHub Desktop.
Forms and Expressions in Clojure

Clojure programs are composed of expressions. Every form not handled specially by a special form or macro is considered by the compiler to be an expression, which is evaluated to yield a value. [1]

An expression is a form which will be (or can be) evaluated in the final program. [2]

For instance, consider the form (fn [x] (inc x)). It is a list of three elements: the special form fn, the vector [x], and the list (inc x). All of those elements are forms, but only the last is an expression, because it is "intended" for evaluation; the first two forms are shuffled around by the macroexpander but never evaluated. The outermost form (fn [x] (inc x)) is itself an expression as well.

This seems to be an interesting distinction, but it does mean it is context-sensitive: [x] is always a form, and may or may not be an expression depending on context. For instance, as in the example above ((fn [x] (inc x))), [x] is a form not an expression because it's handled specially by the special form fn; but in (let [x 1, y [x]] y), however, [x] is an expression as it's evalauted to calculate the value of y.

[1] Clojure Reference - Evaluation

[2] Slightly modified version based on https://stackoverflow.com/a/17223015/8095609

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment