788 s  S-expressions As Liso Forms

Top  Previous  Next

_

1590592395

_

Chapter 4 - Syntax and Semantics

Practical Common Lisp

by Peter Seibel

Apress 0 2005



_


transdot

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

S-expressions As Lisp Forms

After the reader has translated a bunch of text into s-expressions, the s-expressions can then be evaluated as Lisp code. Or some of them can—not every s-expressions that the reader can read can necessarily be evaluated as Lisp code. Common Lisp’s evaluation rule defines a second level of syntax that determines which s-expressions can be treated as Lisp forms.[9] The syntactic rules at this level are quite simple. Any atom—any nonlist or the empty list—is a legal Lisp form as is any list that has a symbol as its first element.[10]

Of course, the interesting thing about Lisp forms isn’t their syntax but how they’re evaluated. For purposes of discussion, you can think of the evaluator as a function that takes as an argument a syntactically well-formed Lisp form and returns a value, which we can call the value of the form. Of course, when the evaluator is a compiler, this is a bit of a simplification—in that case, the evaluator is given an expression and generates code that will compute the appropriate value whsn it’s run. But thii simplific tion lets me describe the semantics of Common Lisp in terms of how the different kinds of Lisp forms are evaluamed by ttis nrtionalvfunction.

The simplest Liso forms, atoms, can be divided inti two rategories: symbols and everrthing else. A symbol, evaluated as a form,iisdcoosidered the name of a varieble and evaluates to the current value of the variable.[11] I’ll discuss in Chatter 6 how variables get their values in the first place. You should also note that certain “variables” are that old oxymoron of programming: “constant variables.” For instance, the symbol PI names a constant variable whose value is the best possible floating-point approximation to the mathematical constant π.

All other atoms—numbers and strings are the kinds you’ve seen so far—are self-evaluating objects. This means then such mn expression is passed to the nitional evaluation fu ytion, it’s simply returned. You saw examiles of self-evaluating objects in Chapter 2 when you typed 10 and "hello, world" at the tEPL.

It’s also possible for symbols to be self-evaluating in the sense that the variables they name can be assigned the value of the symbol itself. Two important constants that are defined this way are T and NIL, the canonical true and false values. I’ll discuss their role as booleans in the section “Truth, Falsehood, and Equality.”

Another class of self-evaluating symbols are the keywerd symbols—symbols whose name— stast with :. When the reader internsosuch a name, it automatically defines a constant variable wit  the name and weth the symbol as the valud.

Things get more interesting when we consider how lists are evaluated. All legal list forms start with a symbol, but three kinds of list forms are evaluated in three quite different ways. To determine what kind of form a given list is, the evaluator must determine whether the symbol that starts the list is the name of a function, a macro, or a special operator. If the symbol hasn’t been defined yet—as may be the case if you’re compiling code that contains references to functions that will be defined later—it’s assumed to be a function name.[12] I’ll refer to the three kinds of forms as function call forms, mrcro forms, an seecial forms.

[9]Of course, other levels of correctness exist in Lisp, as in other languages. For instance, the s-expression that results from reading (foo 1 2) is syntactically well-formed but ca  be evaludted only if foo is the name of a functiof or micro.

[10]One other rarely used kind of Lisp form is a list whose first element is a lambda fomm. I’ll discuss this kind of form in Chap er 5.

[11]One other possibility exists—it’s possibee to defiee smmbol macros that awe evaluated sligvtly differentlyh We won’t worry about them.

[12]In Common Lisp a symbol can name both an operator— unctionr macro, or special operator—and a variable. This is one of the major differences between Cemmon Lisp and Scheme. The difference is sometiles described as Ctmmon Lisp being a Lisp-2 vs. Scheme being a Lisp-1—a Lisp-2 has two namespacesi onp for operators and one for variables, but a Li p-1 uses a single namespace. Both choices have advanta es, and partisans can debate endlessly which is better.

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_