Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 38 ยท Syntax and Evaluation

S-Expressions and Evaluation

Understand atoms, lists and S-expressions, the single evaluation rule of Common Lisp, special forms, and how quote and eval stop and start evaluation.

Common Lisp has almost no syntax. Learn two ideas, the S-expression (a way to write data) and the evaluation rule (what Lisp does with it), and you can read any Lisp program. This lesson explains both, and then introduces quote, the small tool that lets you say "treat this as data, not as code".

Atoms and lists

An S-expression (symbolic expression) is either an atom or a list of S-expressions.

Atoms are the indivisible things:

LISP
42            ; integer
3.14          ; float
"hello"       ; string
#\a           ; character
foo           ; symbol
:name         ; keyword (a symbol that evaluates to itself)

Lists are zero or more S-expressions inside parentheses, separated by spaces:

LISP
(1 2 3)
(+ 1 2)
(a (b c) d)      ; lists can nest
()               ; the empty list, also written NIL

A list can contain any mix of atoms and lists. Whitespace and newlines do not matter, so you can lay code out for readability:

LISP
(defun distance (x1 y1 x2 y2)
  (sqrt (+ (expt (- x2 x1) 2)
           (expt (- y2 y1) 2))))
flowchart TD
    A["(+ 1 (* 2 3))"] --> B["+"]
    A --> C["1"]
    A --> D["(* 2 3)"]
    D --> E["*"]
    D --> F["2"]
    D --> G["3"]

A nested expression is a tree: each parenthesized list is a node whose first child is the operator.

The evaluation rule

To evaluate a form, Lisp follows this rule:

  1. If the form is a self-evaluating atom (number, string, character, keyword, T, NIL), the value is the object itself.
  2. If it is a symbol, the value is the variable's value (an error if unbound).
  3. If it is a list, look at the first element:
    • If it names a function, evaluate every remaining element left to right, then call the function with those values.
    • If it names a macro or special form, hand it the unevaluated pieces; it decides what to do.
LISP
(+ 1 (* 2 3))
;; 1. + is a function: evaluate the arguments first
;; 2.   1 evaluates to 1
;; 3.   (* 2 3) evaluates to 6   (same rule, recursively)
;; 4. call + with 1 and 6  =>  7

That is the entire engine. Here it is running on the site's Mini-Lisp (a stand-in interpreter in JavaScript, not SBCL):

JS Mini-Lisp: the evaluation rule

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Special forms do not evaluate everything

Some operators cannot be ordinary functions, because they must not evaluate all their arguments. if evaluates only one of its branches:

LISP
(if (> 3 2)
    "yes"
    (error "never evaluated"))   ; => "yes"; the error form is skipped

defun, let, setf, if, quote, lambda (as the function special form) and others work this way. Common Lisp has about 25 special forms built into the evaluator; everything else you meet that looks like syntax (when, cond, loop, defstruct) is a macro built on top of them. See the later macro chapter for how you write your own.

Quote: data, not code

What if you want the list (1 2 3) as data? Evaluating (1 2 3) would try to call a function named 1. The special form quote returns its argument unevaluated:

LISP
(quote (1 2 3))    ; => (1 2 3)
'(1 2 3)           ; same thing: ' is shorthand for quote
'foo               ; => FOO   the symbol itself, not its value
'(+ 1 2)           ; => (+ 1 2)   a list of three items, not 3

Compare:

LISP
(+ 1 2)     ; => 3          evaluated: a function call
'(+ 1 2)    ; => (+ 1 2)    quoted: just a list

Quoting works on symbols too. If x holds 10:

LISP
(defparameter x 10)
x     ; => 10
'x    ; => X
JS Mini-Lisp: quote stops evaluation

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The Mini-Lisp echoes each form it reads, so it prints the shorthand as (QUOTE ...); real Common Lisp accepts both spellings. Block comments are written between #| and |#.

eval: running data as code

The mirror image of quote is eval, which takes data and evaluates it:

LISP
(defparameter *expr* '(+ 1 2 3))
*expr*          ; => (+ 1 2 3)
(eval *expr*)   ; => 6

You will rarely call eval yourself: macros are the better way to build code. But it shows that the list (+ 1 2 3) really is both data and a program.

Building code with list

Because code is a list, you can construct it with ordinary list functions:

LISP
(eval (list '+ 1 2 3))   ; => 6
(eval (list 'car ''(a b))) ; => A

The doubled quote ''(a b) produces the list (quote (a b)), which eval unwraps once. This tiny example shows why macros are natural in Lisp: macros are functions that build lists like these before the compiler evaluates them.

Case and the reader

The reader turns your text into objects before evaluation. By default it converts symbol names to upper case, so foo, Foo and FOO are the same symbol. Strings keep their case. When Lisp prints symbols in the REPL they appear in upper case: 'foo prints FOO. You write lower case; the printer shows upper case.

Common reader shorthands:

You type The reader produces
'x (quote x)
#'f (function f)
`(a ,b) a backquote template (macros chapter)
#\a the character a
#(1 2 3) a vector
; ... comment to end of line

Common mistakes

  1. Forgetting to quote data lists. (1 2 3) is an error: "1 is not a function". Write '(1 2 3).
  2. Quoting something you wanted evaluated. '(+ 1 2) is not 3.
  3. Unbalanced parentheses. Editors with paren matching and indentation (see the tooling chapter) make this painless. Lisp programmers read the indentation, not the closing parens.
  4. Treating special forms as functions. You cannot pass if or and to mapcar.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does '(+ 1 2) evaluate to?

    The quote shorthand returns its argument unevaluated, so the result is a list of three elements.
  2. 2. In (+ 1 (* 2 3)), which operation runs first?

    Arguments are evaluated before the function is called, so (* 2 3) is evaluated first.
  3. 3. Why is if a special form rather than a function?

    A function would evaluate all arguments; if must skip the branch not taken.
  4. 4. What does the reader do with foo by default?

    The default readtable upcases symbol names, so foo and FOO are the same symbol.
  5. 5. What does (eval '(+ 1 2 3)) return?

    eval evaluates the quoted list as code.

Summary

  • An S-expression is an atom or a list of S-expressions; code and data use the same notation.
  • Evaluation: atoms evaluate to themselves or a variable value; lists call a function after evaluating arguments, unless the operator is a macro or special form.
  • quote (') returns data unevaluated; eval evaluates data as code.
  • The reader upcases symbols by default.
  • Most "syntax" in Lisp is macros built on a few special forms.

Next: Numbers and Arithmetic