【发布时间】:2013-07-28 22:06:13
【问题描述】:
我正在尝试在 elisp 中创建一个返回另一个函数的函数。我看了一个类似问题的答案(how to return function in elisp),但不明白答案(我今天才刚刚开始学习 elisp,所以请原谅我的无知)。我认为一个更简单的例子会有所帮助。首先,考虑一个测试一个数是否能被 5 整除的函数:
(defun divisible-by-5 (x)
;; tests whether a number is divsible by 5.
(setq remainder (% x 5))
(if (= remainder 0) 1 0)
)
这很好用:
(divisible-by-5 25)
1
现在假设我想创建一个可以创建更多此类测试函数的函数——类似于:
(defun divisible-by-z (z)
(lambda (z)
(setq remainder (% x z))
(if (= remainder 0) 1 0))
)
这不工作。例如,
(defun divisible-by-3 (divisible-by-z 3))
(divisible-by-3 4)
返回错误。我认为即使看到一个关于如何实现这种模式的 elisp 惯用示例也会有所帮助。
【问题讨论】:
标签: elisp