Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not quoting lambda?

Tags:

emacs

elisp

I was told that I shouldn't quote lambda in, say,

(global-set-key (quote [f3]) '(lambda ()   (interactive) (other-window -1) ))

I tried that indeed if I don't quote lambda, it works equally well

(global-set-key (quote [f3]) (lambda ()   (interactive) (other-window -1) ))

However, I don't understand why the latter works (and is also being preferred, and now that the latter works, why the former also works).

If the lambda expression is defined as an other function, we would have called

(global-set-key (quote [f3]) 'my-function)

to prevent my-function to be evaluated immediately. I understand the lambda expression as an anonymous version of my-function. So why shouldn't lambda be quoted?

Thanks!

like image 814
Yi Wang Avatar asked Jan 06 '14 10:01

Yi Wang


1 Answers

Using C-h f lambda <RET>:

A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is self-quoting; the result of evaluating the lambda expression is the expression itself.

So, this answers the question, why you don't need to quote the lambda expression. As to why you shouldn't do it... I think, this has to do with byte compilation. A quoted lambda expression is simply plain data. The byte code compiler has no choice but to include the expression as a constant list literal into its output. An unquoted lambda expression, on the other hand, can be compiled to byte code, resulting in faster execution.

List literals of the form (lambda (...) ...) are special-cased in emacs lisp evaluator and can be used as functions. That's why it works, regardless of whether you quote the lambda expression or not.

like image 194
Dirk Avatar answered Nov 03 '22 14:11

Dirk