Next: , Previous: , Up: Quoting   [Contents][Index]


14.4.2 Quoting with function

The special form function returns its single argument, as written, without evaluating it, and indicates thereby that this argument is to be treated as function. This is mandatory only if you intend to compile your program and want the byte-compiler to optimise your code accordingly. Any other lisp code will accept both the ordinarily quoted and the function-quoted form. Because of this SXEmacs does allow you to evaluate the value cell of a function-quoted object. However, this is bad style and may lead to problems in case of byte-compilation.

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 SXEmacs 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 Quoting with quote.

As for quote, there is also an abbreviated read syntax for function. A sharpsign character followed by an apostrophe (‘#'’) followed by a a Lisp object in read syntax expands to a list whose first element is function, and whose second element is the object. Thus, the read syntax #'y is an abbreviation for (function y).

This effect can be used in macros to distinguish explicitly between variable and function bindings, see example below, also see Macros. However, since the rest of SXEmacs accepts both the quoted and the function-quoted form equally and coerces them to the desired form internally, macros or functions which diverge from this norm should contain a clear remark in their docstring.

Here are some examples of expressions that use function:

(function car)
  ⇒ car
#'car
  ⇒ car
'#'car
  => (function car)
(symbol-function #'funcall)
  ⇒ #<subr funcall>
(symbol-value #'nil)
  ⇒ nil

As mentioned above, an example for a macro which distinguishes between ‘'’ and ‘#'’.

(defmacro picky-funcall (symbol &rest arguments)
  (let ((type (car-safe symbol))
        (name (car (cdr-safe symbol)))
        (qargs (list 'quote arguments)))
    (cond ((eq type 'function)
           (list 'apply symbol qargs))
          ((eq type 'quote)
           (list 'apply name qargs))
          (t
           (error "I don't know what to do with `%s'." symbol)))))

Now we define both a function with the name ‘test’ and a variable with the name ‘test’. Afterwards we apply each to our picky-funcall.

(defun test (a b)
  (+ a b))
  ⇒ test
(defvar test '-)
  ⇒ test
(picky-funcall #'test 4 2)
  ⇒ 6
(picky-funcall 'test 4 2)
  ⇒ 2
(picky-funcall test 4 2)
error→ Cannot decide if `test' contains a variable or function.

Again, the behaviour of picky-funcall is unusual in SXEmacs, you should try to avoid it wherever you can. In contrast, quoting functions with ‘#'’ (or the function form) is usual and even recommended because it clarifies your intention to both human readers and the lisp expression reader.

Also note, in a function definition you have no definite chance to distinguish between any of the quoting forms. This would revert function indirection anyway. However, if you intend to treat variables different from functions you could try to guess using symbol-name, symbol-function and symbol-value (see Symbols).

As for quoting, self-evaluating forms are not affected by function quotation. However, function quoting such forms is most likely wrong anyway so we will not give examples here.

Nonetheless there is a macro definition to turn lambda-lists (see Anonymous Functions) into self-evaluating objects.

(lambda (x) (+ x x))
  ⇒ (lambda (x) (+ x x))

The true expansion of (lambda …) is (function (lambda …)).


Next: , Previous: , Up: Quoting   [Contents][Index]