为什么在emacs-lisp中的函数参数之前使用# [英] Why use #' before function arguments in emacs-lisp?

查看:128
本文介绍了为什么在emacs-lisp中的函数参数之前使用#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很熟悉Emacs Lisp,但不熟悉Lisp。一些Lisp程序员建议(例如 emacs的基本功能)在Lisp代码中的函数参数前面使用#'是很好的。例如:

 (mapc#'my-fun'(1 2 3))

在Emacs Lisp中,我相信这相当于

 code>(mapc'my-fun'(1 2 3))

从elisp手册,第12.7节。


读取语法#'是使用功能
以下表单都是等价的:

 (lambda(x)(* xx))
函数(lambda(x)(* xx)))
#'(lambda(x)(* xx))


函数的帮助


函数是 eval.c 中的一种特殊形式。

 (功能ARG)

喜欢 quote ,但优选作为功能的对象。在字节
编译中,函数导致其参数被编译。 报价
不能这样做。


所以看起来像潜在的优化,但没有更多。而且,从ML / Haskell的背景来看,将功能与任何其他数据不同的方式看起来很奇怪。



问题:



你同意#'应用于 emacs-lisp 函数参数?
(简要说明在Common Lisp中为什么需要它们也是很棒的。)



注意:



我以为可能会忽略#'(值vs函数)时可能会读取不同的单元格。但是
这似乎是错误的,因为函数的文档对于获取函数单元没有任何意义。这是通过使用 symbol-function 实现的。



相关问题是





但他们似乎建议,#'是不必要的,至少在lambdas。

解决方案

#'foo 中的引号与'foo



#'foo 阅读时间由(function foo)。当它被编译和执行时,它查找由 foo 命名的功能定义(由$ code> defun , flet 标签或类似的)。



'foo 阅读时间(报价foo)替换。当它被编译和执行时,它只是被符号 foo 替换。



Funcall 应用(因此通常是较高阶的函数)将函数指示符作为参数。功能指示符可以是一个函数或一个命名函数的符号,所以#'foo 'foo 是函数指示符。



因此,表单'foo #'foo 似乎是可以互换的乍一看。然而,真正的函数的查找是在不同的时间完成的,而$ code>#'foo 被调用在哪里,而 foo 只有在最终应用时才会被查找。



如果您使用函数指示符多次,则更有效率做一次查找,甚至在编译时。这可能是一个真正的大时代,并在编辑器中转化为更平滑的用户体验。


I'm familiar with Emacs Lisp, but not Common (or any other) Lisp. Some Lisp programmers suggest (e.g. A basic function for emacs) that it's good to use #' in front of function arguments in Lisp code. For example:

(mapc #'my-fun '(1 2 3))

In Emacs Lisp, I believe that this is equivalent to

(mapc 'my-fun '(1 2 3))

From the elisp manual, section 12.7.

The read syntax #' is a short-hand for using function. The following forms are all equivalent:

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

and the help for function

function is a special form in eval.c.

(function ARG)

Like quote, but preferred for objects which are functions. In byte compilation, function causes its argument to be compiled. quote cannot do that.

So it seems like a potential optimization, but no more. Moreover, coming from an ML/Haskell background, it seems strange to treat a function differently from any other data.

Question:

Do you agree that #' should be used in emacs-lisp function arguments? (A brief explanation of why they're needed in Common Lisp would be great as well.)

Notes:

I thought it may read a different cell when the #' is omitted (value vs function). But this seems wrong because the documentation for function doesn't say anything about grabbing the function cell. This is achieved by using symbol-function.

Related questions are

but they seem to suggest that the #' is unnecessary, at least in lambdas.

解决方案

The quote character in #'foo has nothing to do with the one in 'foo.

#'foo is replaced at read time by (function foo). When that is compiled and executed, it looks up the functional definition named by foo (by defun, flet, labels or similar).

'foo is replaced at read time by (quote foo). When that is compiled and executed, it is simply replaced by the symbol foo.

Funcall and apply (and thus generally higher order functions) take function designators as arguments. A function designator can be a function or a symbol naming a function, so both #'foo and 'foo are function designators.

Therefore, the forms 'foo and #'foo seem interchangable at first glance. However, the lookup of the real function is done at different times—#'foo is looked up where it is invoked, while the function named by 'foo would only be looked up when it is finally applied.

If you use a function designator many times over, it is much more efficient to do the lookup just once, or even just at compile time. That can be a really big timesaver, and translates to a smoother user experience in an editor.

这篇关于为什么在emacs-lisp中的函数参数之前使用#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆