next up previous
Next: Primitive and Built-in Functions Up: Emacs Lisp, The Language Previous: Misc stuff about functions

Special Forms

In lisp terminology, a ``form'' is the same as an ``expression''. We saw above that for defun to behave ``correctly'', its arguments should not be evaluated. In effect, we are asking for a semantics that is slightly different from the typical run-of-the-mill function call. An expression which has this special semantics is called a ``special form''. Thus, a special form is, by definition, a function specially marked such that not all of its arguments are evaluated. Most special forms define control structures or perform variable bindings - things that functions cannot do. defun is the only special form that we have encountered thus far. The total number of available special forms is quite small, but we shall not look into all of them, rather, in order to give you some more feel about the need for special forms, we shall look a couple more.

In most modern languages the boolean operators or and and (``||'' and ``&&'' in C, C++ and Java) have a property known as ``short-circuiting''. This short circuiting property enables us to safely write code that looks like this:

if (p && p->data == 0) {
  // ...
}

In the above code snippet, the second expression p->data == 0) is evaluated only if necessary. The and function in lisp also has this short-circuiting property, and the lisp version looks something like this:

(and (not (null p))
     (= data 0))

In order to effect this short-circuiting property, we cannot allow the normal evaluation semantics of a typical function call as that will evaluate both the sub expressions. For this reason or is implemented as a special form in Elisp. The advanced reader might be interested to know that in Common Lisp, many of the constructs that are special forms in Elisp special forms are actually implemented as macros.

Early on, when talking about types of expressions, we mentioned that the variable assignment expression (setq foo 10) is special, but did not elaborate. With your new found enlightenment, you should have no trouble seeing that setq is a special form. For, if it were a regular function, the first argument foo would be evaluated, and we would not quite be able to assign a new value to it.

The Emacs Lisp Manual lists the complete list of available special forms in Elisp.


next up previous
Next: Primitive and Built-in Functions Up: Emacs Lisp, The Language Previous: Misc stuff about functions
Sriram Karra
2005-01-06