如何在Emacs Lisp中做闭包? [英] How do I do closures in Emacs Lisp?

查看:119
本文介绍了如何在Emacs Lisp中做闭包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在JavaScript和其他现代命令式语言中,我将使用闭包:

p>

 函数id(a){
return function(){return a;};
}

但是Emacs lisp不支持这些。



我可以创建身份函数和部分函数应用程序的组合,但它不支持。



那么如何做呢? >

解决方案

愚蠢的想法:如何:

 `(lambda(),x))

(funcall(foo 10));; => 10


I'm trying to create a function on the fly that would return one constant value.

In JavaScript and other modern imperative languages I would use closures:

function id(a) {
    return function() {return a;};
}

but Emacs lisp doesn't support those.

I can create mix of identity function and partial function application but it's not supported either.

So how do I do that?

解决方案

Stupid idea: how about:

(defun foo (x)
  `(lambda () ,x))

(funcall (foo 10))  ;; => 10

这篇关于如何在Emacs Lisp中做闭包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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