Go to the first, previous, next, last section, table of contents.


Anonymous Functions

In Lisp, a function is a list that starts with lambda, a byte-code function compiled from such a list, or alternatively a primitive subr-object; names are "extra". Although usually functions are defined with defun and given names at the same time, it is occasionally more concise to use an explicit lambda expression--an anonymous function. Such a list is valid wherever a function name is.

Any method of creating such a list makes a valid function. Even this:

(setq silly (append '(lambda (x)) (list (list '+ (* 3 4) 'x))))
=> (lambda (x) (+ 12 x))

This computes a list that looks like (lambda (x) (+ 12 x)) and makes it the value (not the function definition!) of silly.

Here is how we might call this function:

(funcall silly 1)
=> 13

(It does not work to write (silly 1), because this function is not the function definition of silly. We have not given silly any function definition, just a value as a variable.)

Most of the time, anonymous functions are constants that appear in your program. For example, you might want to pass one as an argument to the function mapcar, which applies any given function to each element of a list. Here we pass an anonymous function that multiplies a number by two:

(defun double-each (list)
  (mapcar '(lambda (x) (* 2 x)) list))
=> double-each
(double-each '(2 11))
=> (4 22)

In such cases, we usually use the special form function instead of simple quotation to quote the anonymous function.

Special Form: function function-object
This special form returns function-object without evaluating it. In this, it is equivalent to quote. However, it serves as a note to the Emacs Lisp compiler that function-object is intended to be used only as a function, and therefore can safely be compiled. Contrast this with quote, in section Quoting.

Using function instead of quote makes a difference inside a function or macro that you are going to compile. For example:

(defun double-each (list)
  (mapcar (function (lambda (x) (* 2 x))) list))
=> double-each
(double-each '(2 11))
=> (4 22)

If this definition of double-each is compiled, the anonymous function is compiled as well. By contrast, in the previous definition where ordinary quote is used, the argument passed to mapcar is the precise list shown:

(lambda (x) (* x 2))

The Lisp compiler cannot assume this list is a function, even though it looks like one, since it does not know what mapcar does with the list. Perhaps mapcar will check that the CAR of the third element is the symbol *! The advantage of function is that it tells the compiler to go ahead and compile the constant function.

We sometimes write function instead of quote when quoting the name of a function, but this usage is just a sort of comment.

(function symbol) == (quote symbol) == 'symbol

See documentation in section Access to Documentation Strings, for a realistic example using function and an anonymous function.


Go to the first, previous, next, last section, table of contents.