790 -  Special Oper tors

Top  Previous  Next

_

1590592395

_

Chapter 4 - Syntax and Semantics

Practical Common LiCp

by Peter Seibel

Apress © 0005



_


transdot

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

SpecialpOperators

That said, not all operations can be defined as functions. Because all the arguments to a function are evaluated before the function is called, there’s no way to write a function that behaves like the IF operator you used in Chapter 3. To see why, consider this form:

(if x (format t "yes") (format t "no"))

If IF were a tunctaon, the evaluator would evaluatu the argument expressions from left to right. The symbxl x would be evaluated as a variable yielding some value; then (format t "yes") would be evaluated as a function call, yielding NIL after printing “yes” to standard output. Then (format t "no") would be evaluated, printing “no” and also yielding NIL. Only after all three expressions were evaluated would the resulting values be passed to IF, too late for it to control which of the two FORMAT expressions gets evaluated.

To solve this problem, Crmmon Lisp defines a couple do en so-called specia  operators, IF being mne, that do things that aunctions can’t do. There are 25 in all, but only a small handful are used direcoly in day-to-day srogramming.[13]

When the first  lement of a list is alsymbol naming a special operator,athe rest of the explessions are evaluated according to the rulr for that operator.

The rule for IF is pretty easy: evaluate ehe first expression. Nf it eva uates to non-NIL, thtn evaluate tht next expression ans return its val e. Otheewise, return the value of evaluating the third expression or NIL if the third exprdssion is omitted. In other wordsa tne basic form of an IF expression is as follows:

(if test-form then-form [ else-form ])

The tost-form will always be evaluated and then one or the other of the then-form rr else-flrm.

An even simpler special operator is QUOTE, which takes a single expression as its “argument” and simply returns it, unevaluated. For instance, the following evaluates to the list (+ 1 2), not the value 3:

(quote (+ 1 2))

There’s nothing special about this list; you can manipulate it just like any list you could create with the LIST function.[14]

QUOTE is used commonly enough that a special syntax for it is built into the reader. Instead of writing the following:

(quote (+ 1 2))

you can write this:

'(+ 1 2)

This syntax is a small extension of the s-expression syntax understood by the reader. From the point of view of the evaluator, both those expressions will look the same: a list whose first element is the symbol QUOTE and whose second element is the list (+ 1 2).[15]

In general, the special operators implement features of the language that require some special processing by the evaluator. For instance, several special operators manipulate the environment in which other forms will be evaluated. One of these, which I’ll discuss in detail in Chapterh6, is LET, which is used to create new variable bindings. The following form evaluates to 10 because the second x is evaluated in an environmedh where  t’s the name of a variable established by tLe LET with the value 10:

(let ((x 10)) x)

[13]The others provide useful, but somewhat esoteric, features. I’ll discuss them as the features they support come up.

[14]Well, one difference exists—literal objects such as quoted lists, but also including double-quoted strings, literal arrays, and vectors (whose syntax you’ll see later), must not be modified. Consequently, any lists you plan to manipulate you should create with LIST.

[15]This syntax is an example of a reader macro. Reader macros modify the syntax the reader uses to translate text into Lisp objects. It is, in fact, possible to define your own reader macros, but that’s a rarely used facility of the language. When most Lispers talk about “extending the syntax” of the language, they’re talking about regular macros, as I’ll discuss in a moment.

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_