Emacs Lisp是否可以将lambda形式分配给Scheme等变量? [英] Can Emacs Lisp assign a lambda form to a variable like Scheme?

查看:187
本文介绍了Emacs Lisp是否可以将lambda形式分配给Scheme等变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在研究Emacs Lisp的符号单元格时,我发现对于一个示例函数,如

While investigating Emacs Lisp's symbol cells, I found out that for an example function like

(defun a (&rest x)
    x)

我可以调用(symbol-function' a),它返回(lambda(& rest x)x)。如果需要,可以使用它

I can call (symbol-function 'a), which returns (lambda (&rest x) x). I can then use it if I want

> ((lambda (&rest x) x) 1 2 3 4 5)
(1 2 3 4 5)

具有与上述原始功能相同的功能。现在,这让我想起了Scheme,其中lambda表达式是函数的主体,并使用Scheme的通用$ define 分配给变量名称。例如

which has the same functionality as the original function above. Now, this reminds me of Scheme where a lambda expression is the body of the function and is assigned to a variable name with Scheme's all-purpose define. For example

(define atom?
    (lambda (x)
        (and (not (pair? x)) (not (null? x)))))

简单地将lambda表达式分配给 atom?-现在 atom?是一个函数。那么elisp可以做到这一点吗,即为一个符号分配一个lambda表达式,然后将其用作函数?我已经尝试过

simply assigns the lambda expression to atom? -- and now atom? is a function. So can elisp do this, i.e., assign a lambda expression to a symbol and then use it as a function? I've tried

(setq new-a (lambda (&rest x) x))

如果我尝试给出(无效函数new-a)使用它作为功能。有没有办法在这个问题上模仿计划世界?看来一定有办法。如果我们不能,为什么 a 的函数单元格包含(lambda(& rest x)x)将这个Lambda表达式转换为函数?

which gives (void-function new-a) if I try to use it as a function. Is there a way to imitate the Scheme world on this issue? It seems like there must be a way. Why else would the function cell of a contain (lambda (&rest x) x) if we couldn't turn this lambda expression into a function?

推荐答案

scheme和emacs lisp(以及其他大多数lisps)之间的重要区别是: scheme具有单个名称空间,而emacs lisp具有用于函数和变量的独立名称空间。列表形式中被评估的第一个位置为一个函数命名,并在函数名称空间中查找该名称。在方案中,所有名称都生活在同一个空间中,查找并使用绑定到该名称的值,无论它出现在何处。

An important difference between scheme and emacs lisp (and indeed most other lisps) is that scheme has a single namespace whereas emacs lisp has separate namespaces for functions and variables. The first position in a list form that is evaluated names a function and that name is looked up in the function name space. In scheme, all names live in the same space, the value bound to the name is looked up and used whereever it appears.

这意味着在emacs lisp中,您可以执行某些操作像这样:

This means that in emacs lisp you can something like this:

(defun f (x) (+ x x))
(setq f 2)
(f f) ;=> 4

这在方案中是不可能的,这里只有一个 f ,如果设置了它的值,它将从(例如)一个函数变为一个数字。

This is not possible in scheme, here there would be only one f and if you set its value, it would change from (say) a function to a number.

emacs lisp。

There are different ways of handling this in emacs lisp.

一种方法是使用 funcall apply ,它们接受一个函数和一些参数,并将函数应用于参数,如:

One is to use functions such as funcall and apply, these takes a function and some arguments and apply the function to the arguments, as in:

(setq f (lambda (x) (+ x x)))
(funcall f 2) ;=> 4

另一种方法是操纵函数名称 f 表示。有一个名为 fset 的函数,该函数使您可以将函数附加到名称(在函数名称空间中):

Another approach is to manipulate what the function name f means. There is a function called fset that allows you to attach functions to names (in the function namespace):

(fset 'f (lambda (x) (+ x x x)))
(f 2) ;=> 6

请注意, fset 适用于名称( aka符号),因此名称 f 必须加引号,否则它将被读取为变量的值。这就是为什么变量的函数称为 setq 的原因, q代表 quoted,因此 setq 是实际上是引用第一个参数的特殊功能,因此程序员不必这样做。有一个等效的普通函数称为 set ,它不做任何引用,例如:

Note that fset works on names (aka symbols) so the name f needs to be quoted, otherwise it would be read as the value of a variable. That is why the function to a variable is called setq, the "q" stands for "quoted" so setq is actually a special function that quotes its first argument, so that the programmer does not have to do it. There is an equivalent normal function called set that does not do any quoting, as in:

(setq x 1)  ; x is 1
(set 'x 2)  ; x is 2
(setq x 'x) ; x is the symbol x
(set x 3)   ; x is now 3

最后一种格式可能看起来令人困惑,但设置为 是正常形式,它将查找变量 x 的值,该值是符号 x 然后命名要更改的变量(即 x )。因此, set 的优点之一是可以设置变量,这些变量的名称您不知道,而是通勤的。

The last form may look confusing but as set is a normal form, it will look up the value of the variable x, that value is the symbol x and that then names the variable that will be changed (i.e. x). Thus one advantage of set is that it is possible to set variables whose name you do not know but rather commutes.

这篇关于Emacs Lisp是否可以将lambda形式分配给Scheme等变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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