Programs often need to refer to literal data values--data that you type directly into the program. In many languages, the only literals are fairly simple values like integers and strings. In Scheme, you can use simple literals or complicated ones that represent (pointers to) data structures like nested lists. Earlier, we showed how to create list literals using quoting.
You've probably noticed that the syntax of Scheme code and the
textual representation of Scheme data are very similar. So,
(min 1 2) is a combination if it's viewed as code,
but it's also the standard textual representation of a list containing
min and the integers
(A symbol is a data object that's sort of like a string, but with some special properties, which will be explained in the next chapter.)
The resemblance between code and data is no accident, and it can be very convenient, as later examples will show. It can be confusing, too, however, so it's important to know when you're looking at a piece of code and when you're looking at a piece of literal data.
The first thing to understand is quoting. In Scheme, the
(min 1 2) is a procedure call to
with the arguments
As we explained earlier, we can quote it by wrapping it in the special
), however, and get a literal list
(min 1 2).
For example, the definition
(define foo (quote (min 1 2)))
defines and binds
foo, initializing the binding with (a pointer
to) the list
(min 1 2).
We can draw this situation this way:
+---+ +---+---+ +---+---+ +---+---+ foo | *-+--->| * | *-+----->| * | *-+----->| * | * | +---+ +-+-+---+ +-+-+---+ +-+-+---+ | | | \|/ \|/ \|/ min 1 2
Of course, as we explained earlier, we can use
' as a euphemism
We can define very complicated literals this way, if we want to. Here's a procedure that returns a nested list of nested lists of integers and booleans and symbols:
(define (fubar) '(((1 two #f) (#t 3 four)) ((five #f 6) (seven 8 #t)) ((#f 9 10)) ((11 12 #f))))
that's a pretty useless procedure, but it's very convenient to just be able to type in printed representations of nested data structures and have Scheme construct them automatically for you. In most languages you'd have to do some fairly tedious hacking to construct a list like that. As we'll see in a later chapter, Scheme also supports quasiquotation, which lets you construct mostly-literal data structures, and create customized variations on them easily; quasiquotation will be discussed in a later chapter.