A "normal" Lisp function call looks like this:
Code: Select all
(<function-name> <argument-1> <argument-2> ... )
The standard evaluation rule for Lisp functions is:
- First evaluate all arguments from left to right
- Then apply the function to the evaluated arguments
Evaluating the arguments first makes it possible to write Lisp code that looks like a tree:
Code: Select all
(function-2 <argument-1> ... )
|
v
(function-1 <argument-1> <argument-2> ... )
^
|
(function-3 <argument-1> ... )
The nice thing is that this simple evaluation rule already covers 90% of all Lisp function calls. This means that Common Lisp is not as big as it looks, because the basics are very simple.
But there are cases where this evaluation rule does not make sense. For example:
Here it doesn't make sense to evaluate the TEST as well as both THEN and ELSE parts first, and only afterwards decide which one of THEN or ELSE should be evaluated. This means that IF needs a "special" evaluation rule, where only the TEST argument is evaluated first, and only then one of THEN or ELSE arguments is evaluated, depending on the result of evaluating the TEST.
The same is with SETQ:
If the SYMBOL argument would be evaluated, then the VALUE would be assigned to old evaluated value of SYMBOL, not to the name of the symbol. Or even worse, an error would be signaled if the symbol doesn't exist. This means that SETQ needs a "special" evaluation rule, where only the VALUE, but not the SYMBOL argument is evaluated.
Every Lisp function that breaks the "evaluate all arguments first" rule needs its own special evaluation rule in the interpreter (or compiler). That's where the "special" comes from. Because such a function breaks the standard evaluation rule for functions, it's not a Lisp "function" anymore (at least according to the Lisp evaluation rules). In the Common Lisp specification such "functions" therefore are called "special operators", but you will also find names like "special forms" in old Lisp texts.
It's also important to know that you cannot define your own "special operators", but you can define Lisp macros if you need to break the evaluation rules. It's also possible that a "special operator" is internally defined as a macro by the Lisp implementation.
But the only important thing here is that whenever something is called "special" this means that it breaks the standard evaluation rules. There are also "special variables", which have dynamic instead of lexical scope.
Martin Kalbfuß wrote:...what's the difference between a macro and a special form?
A special operator is a simple function call with special evaluation rules. A macro additionally can perform complex syntax transformations by generating Lisp code. Macros are extensively explained in Paul Graham's
On Lisp (free PDF download link after the first text section on that page).
Martin Kalbfuß wrote:Why is defun a macro but setq not?
Because DEFUN changes the syntax by generating new code:
will generate code that looks like this:
Code: Select all
(progn
(intern (symbol-name (quote add1)))
(setf (symbol-function (quote add1)) (lambda (x) (1+ x))))
Many Common Lisp macros are syntactic sugar for ugly-looking code. See
MACROEXPAND that will show you the code generated by Lisp macros.
- edgar