typestar

Quoting in Lisp

A quote is the difference between running (+ 2 3) and holding on to it.

;; quote holds a form back instead of running it
(defparameter *sum* (+ 2 3))
(defparameter *unrun* '(+ 2 3))
(format t "evaluated ~a, quoted ~a~%" *sum* *unrun*)

;; symbols are ordinary data once quoted, not just names of variables
(defparameter *weekdays* '(monday tuesday wednesday))
(format t "second day: ~a~%" (second *weekdays*))
(format t "spelled out: ~a~%" (symbol-name 'friday))

;; eq asks whether two things are the same object, equal compares shape
(format t "eq on symbols: ~a~%" (eq 'monday 'monday))
(format t "eq on fresh lists: ~a~%" (eq (list 1 2) (list 1 2)))
(format t "equal on fresh lists: ~a~%" (equal (list 1 2) (list 1 2)))

How it works

  1. (+ 2 3) evaluates to 5 while '(+ 2 3) stays a three-element list.
  2. '(monday tuesday wednesday) treats symbols as data, reachable with second and readable with symbol-name.
  3. eq is true for two references to 'monday but false for two fresh lists that equal accepts.

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
651
Tokens
103
Three-star pace
70 tpm

At the three-star pace of 70 tokens a minute, this run takes about 88 seconds.

Type this snippet

Step 3 of 3 in Lists & conses, step 6 of 27 in Language basics.

← Previous Next →