Code Is Data

A quoted list and a call are the same shape, and understanding that one fact explains most of what makes this family of languages different.

Result. Explain why a program in this language is made of the same lists that it manipulates, and why that is useful rather than a curiosity.

What you need first. Reading the Parentheses. Nothing else.

The same shape means two things

You already know this shape.

LISPEX
(+ 2 3)

When run, it produces five. Now put a quote mark in front of it.

LISPEX
'(+ 2 3)

That returns a list of three items, the symbol +, the number two, and the number three. Nothing was added or evaluated. The quote says to treat this as data rather than something to do.

The same text is a call or a list, depending only on whether you ask it to run. In most languages, a function call and a list of three items are unrelated concepts that happen to share punctuation. Here, they are one thing seen two ways.

Why that matters in ordinary code

You do not need to write a compiler for this to pay off. It shows up the first time a program returns a decision instead of a number.

Say you put a two item list under the name answer. Two names pull values back out of a list. car returns the first item, and cdr returns everything after the first item, still as a list. Neither name tells you what it does, because both came straight from register names on the machine Lisp first ran on. Do not try to decode them. Just learn the two.

LISPEX
(define answer '(decision allow))
(car (cdr answer))

cdr hands back the one item list (allow), and car takes the item out of it, so the result is allow. The program built a small piece of structured data using the exact notation it is written in, and pulled the piece it needed back out with ordinary list operations. There was no separate syntax for literals, no extra format to convert data into before saving or sending it, and no object model to define first.

That is the everyday version of the idea. A program that produces structure creates the same kind of thing it is built from, so the tools for one work on the other.

Quote, and the family around it

Quote has relatives you will meet later. The four marks below are notation, so read them rather than run them.

'x
`x
,x
,@xs

The first is quote, treat as data. The second is quasiquote, treat as data but allow holes. The third is unquote, which fills a hole with a computed value, and the fourth is unquote-splicing, which fills a hole by splicing in a list. They exist so a program can build a structure that is mostly fixed, with a few parts filled in.

You do not need them yet. Knowing they exist keeps you from being surprised when a backtick appears that is not a string.

The honest caveat

The famous consequence of code being data is that programs can write programs. That is real, and it is also not what most working code does. Most working code benefits in the smaller way shown above, with uniform notation and one set of tools.

Treat the grand version as a door that is open rather than a room you must enter.

Try this

Predict both results before running them.

LISPEX
(list (+ 1 2) '(+ 1 2))
Show answer

(3 (+ 1 2)). The first element was evaluated and became three. The second was quoted, so it stayed a three item list. The same text, two outcomes, decided entirely by the quote mark.

Ready to move on when

You can explain what the quote mark does, and give an example of a program returning structured data. You can also explain why using the same notation for data and calls is convenient rather than confusing.

Continue to Data, Conditions, and Decisions, which builds real rules out of exactly these pieces.