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

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

问题描述

我正在尝试动态创建一个返回一个常量值的函数.

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

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

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

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

但 Emacs lisp 不支持这些.

but Emacs lisp doesn't support those.

我可以创建身份函数和部分函数应用程序的混合,但也不支持.

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

那我该怎么做?

推荐答案

愚蠢的想法:怎么样:

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

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

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

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