[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

12.7 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 define a function change-property which uses a function as its third argument:

 
(defun change-property (symbol prop function)
  (let ((value (get symbol prop)))
    (put symbol prop (funcall function value))))

Here we define a function that uses change-property, passing it a function to double a number:

 
(defun double-property (symbol prop)
  (change-property symbol prop '(lambda (x) (* 2 x))))

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

 
(defun double-property (symbol prop)
  (change-property symbol prop
                   (function (lambda (x) (* 2 x)))))

Using function instead of quote makes a difference if you compile the function double-property. For example, if you compile the second definition of double-property, the anonymous function is compiled as well. By contrast, if you compile the first definition which uses ordinary quote, the argument passed to change-property 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 change-property will do with the list. Perhaps it will check whether the CAR of the third element is the symbol *! Using function tells the compiler it is safe to go ahead and compile the constant function.

Nowadays it is possible to omit function entirely, like this:

 
(defun double-property (symbol prop)
  (change-property symbol prop (lambda (x) (* 2 x))))

This is because lambda itself implies 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

The read syntax #' is a short-hand for using function. For example,

 
#'(lambda (x) (* x x))

is equivalent to

 
(function (lambda (x) (* x x)))

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 9.3 Quoting.

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


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on May 2, 2002 using texi2html