Why not quoting lambda?

老子叫甜甜 提交于 2019-12-10 02:25:36

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/20948325/why-not-quoting-lambda

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!