1

To me these operators seem to do the same thing. Both take a symbol and return the function associated with it. Is there any difference?

elisp evaluation returns the following:

(defun foo (x) (+ 1 x))
foo
(foo 3)
4
#'foo

Which I don't understand either.

Furthermore is there a difference between common lisp and elisp? I'm learning from resources on either.

djechlin
  • 59,258
  • 35
  • 162
  • 290
  • You may wish to remove the elisp tag from this question -- I don't think you tried this out in Emacs before you asked. – phils Mar 28 '15 at 21:41
  • @phils I don't understand elisp's behavior either, see edit. – djechlin Mar 29 '15 at 19:18
  • In elisp `function` (`#'`) is largely equivalent to `quote` (`'`) and hence it does *not* return the function definition for a symbol; it just returns the symbol. There are still reasons to use it (historically the benefit was restricted to byte-compilation, and nowadays there are also reasons related to lexical binding), but your specific question title makes very little sense for elisp. – phils Mar 30 '15 at 00:30
  • I go further to suggest you learn one Lisp (at least the basics), perhaps the one that gives you the greatest return in short-term, then learn another. Learning two Lisps in an early stage and keep comparing them will only confuse you, a lot. Actually, this fits well for any two similar languages. – acelent Mar 30 '15 at 09:05

2 Answers2

7

Common Lisp:

SYMBOL-FUNCTION can't retrieve functions from lexically bound functions. FUNCTION references the lexically bound function by default. #'foo is just a shorter notation for (FUNCTION foo).

CL-USER 1 > (defun foo () 'foo)
FOO

CL-USER 2 > (flet ((foo () 'bar))
              (list (funcall (symbol-function 'foo))
                    (funcall #'foo)
                    (funcall (function foo))
                    (eq (function foo) (symbol-function 'foo))))
(FOO BAR BAR NIL)

CL-USER 3 > (eq (function foo) (symbol-function 'foo))
T
Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
4

Rainer's answer discusses the things that you can do with function that you can't do with symbol-function, namely retrieve the value of lexically scoped functions, but there are a few other differences too.

The special operator FUNCTION

The special operator FUNCTION provides a way to find the functional value of a name in a lexical environment. Its argument is either a function name or a lambda expression. That function can take a lambda expression means that you can write: (function (lambda (x) (list x x))). The term function name includes more than just symbols. It also includes lists of the form (setf name), which means that you can do things like (function (setf car)).

The accessor SYMBOL-FUNCTION

The accessor symbol-function, on the other hand, lets you retrieve and set the functional value of a symbol. Since it requires a symbol, you can't do (symbol-function (lambda …)) or (function (setf name)). Symbol-function also can't see lexical environments; it only works with global definitions. E.g.,

(flet ((foo () 'result))
  (symbol-function 'foo))
;=> NIL

Since symbol-function is an accessor, you can change the value of a function's symbol with it. E.g.:

CL-USER> (setf (symbol-function 'foo) (lambda () 42))
#<FUNCTION (LAMBDA ()) {1005D29AAB}>
CL-USER> (foo)
42
CL-USER> (setf (symbol-function 'foo) (lambda () 26))
#<FUNCTION (LAMBDA ()) {1005D75B9B}>
CL-USER> (foo)
26

The accessor FDEFINITION

There's also the accessor fdefinition which is kind of like symbol-function in that is can only access globally defined functions, but kind of like function in that it can access of symbols and (setf name) lists. However, it does not retrieve the value of lambda functions.

Community
  • 1
  • 1
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • Is there a distinction between a symbol and name? I'm starting to wonder if I understand lexical scope less than I thought. – djechlin Mar 29 '15 at 17:50
  • @djechlin A symbol is an actual object. You can inspect it, you can create them with make-symbol, etc. In source code (as data), symbols occur very frequently. However, lexical variables, e.g., `x` in `(let ((x 42)) x)`, while represented by symbols in the source code, don't actually have any connection with the symbol at runtime. The compiled version of `(let ((x 42)) x)` doesn't need to know anything about the symbol `x`. Intuitively, this makes sense, because we'd expect the code `(let ((y 42)) y)` to compile to the same thing. (However, when a variable is special, there *is* a – Joshua Taylor Mar 29 '15 at 19:44
  • @djechlin connection with the symbol.) As to the difference between variable names and symbols: *most* of the time that you need a function name, you'll be interested in something that's a symbol (e.g., `car` or `make-person`). But lists of the form `(setf )` are also function names. You can do, for instance, `(funcall #'(setf car) ...)`. See [the glossary entry for *function name*](http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_f.htm#function_name). – Joshua Taylor Mar 29 '15 at 19:47