【发布时间】:2011-10-27 08:09:08
【问题描述】:
apply-partially 的 Emacs 代码是这样的:
(defun apply-partially (fun &rest args)
"Return a function that is a partial application of FUN to ARGS.
ARGS is a list of the first N arguments to pass to FUN.
The result is a new function which does the same as FUN, except that
the first N arguments are fixed at the values with which this function
was called."
`(closure (t) (&rest args)
(apply ',fun ,@(mapcar (lambda (arg) `',arg) args) args)))
它返回一个看起来很像 lambda 表达式的列表,除了 lambda 被 closure (t) 替换。例如,(apply-partially 'cons 1) 返回:
(closure (t) (&rest args) (apply (quote cons) (quote 1) args))
据我所知,它的外观和工作方式完全一样:
(lambda (&rest args) (apply (quote cons) (quote 1) args))
除了“闭包”表达式没有 lambda 的“自引用”属性,所以当我尝试评估它时,Emacs 告诉我closure 没有函数定义:Lisp error: (void-function closure)。
我在 Elisp 手册中找不到以这种方式使用符号 closure 的任何参考。这似乎是某种 Emacs 内部的魔法。显然没有根据正常规则评估闭包表达式(因为手动执行此操作会出错)。
那么这里发生了什么?我是否需要对 C 代码中的“闭包”引用进行 grep 查找?
编辑:似乎在 Emacs 23 及更低版本中,apply-partially 只是使用 cl 包中的 lexical-let 来进行闭包。以上定义来自“24.0.90.1”版本。
【问题讨论】: