schemethe-little-schemer

Ambiguity with `quote` in the Little Schemer. When should an expression be proceeded with a quote and when shouldn't it?


The Little Schemer 4e by Friedman and Felleisen starts out by defining atom?:

(define atom?
    (lambda (x)
        (and (not (pair? x)) (not (null? x)))))

Then in the first chapter there's questions asking if so and so is a atom or not. E.g. (the footnote is rephrased for clarity):

Is it true that this is an atom?

atom*

*in Scheme: (quote atom) or 'atom

It says turkey, 1492, u, and *abc$ are also atoms.

All of these except 1492 must be preceded by a ' - otherwise we get a Error: execute: unbound symbol:

1492 and '1492 both work. Henceforth, in this book, how do I know when something should or should not be preceded by a quote (')??? It's confusing. I wish the authors had just been explicit with the quotes -- does it really add that much visual noise to have a single quote (') precede expressions??? Should I just assume everything is preceded by a quote (')?


Solution

  • Atoms. An atom is an indivisible thing. A value like 13 or 72 is indivisible, it is a single value. There are other atoms, a string like "hello", a variable like x or y, a value like :keyword or 'something.

    When Lisp is written, atoms are separated by spaces or parentheses. We can then create one or more form by putting atoms within parentheses, for example (* 2 (+ 1 3)). The atoms here are:

    There are then a couple of problems. For starters, with x do we mean the atom 'x, a variable called x, or the value stored within the variable x? Lisp assumes that x is a variable and so typing x returns the value of x or produces an error about x being unbound. Putting a single quote in front of x, as 'x, says "please don't try to evaluate this".

    Forms have a consistent format (operator data1 data2 data3 ...). But what happens if you just want to supply data? What operator do you use? The answer is to use operator quote, which evaluates the form to just the data. Because saying quote all of the time is annoying, Lisp allows you to say'(data1 data2 data3 ...) instead.

    As for '1492, this is the same as 1492 in Scheme and Common Lisp. The values are interchangeable. I would use 1492 since the quote is superfluous.