小编tip*_*tto的帖子

如何在 Emacs Lisp 中动态更改 lambda 内容?

我有两个返回匿名函数的函数,除了几行之外,函数中的几乎所有内容都是相同的。

(defun foo ()
  (interactive)
  (lambda (arg)
    (when (not (string-empty-p arg))
      (message "foo")
      (message arg))))

(defun bar ()
  (interactive)
  (lambda (arg)
    (when (not (string-empty-p arg))
      (message "bar")
      (message arg))))
Run Code Online (Sandbox Code Playgroud)

因此,我创建了一个函数,该函数接收列表作为参数并返回一个匿名函数,如下所示:

(defun make-anonymous-func (&rest body)
  (interactive)
  (lambda (arg)
    `(when (not (string-empty-p arg))
      ,@body
      (message arg))))
Run Code Online (Sandbox Code Playgroud)

但是,当我执行 返回的函数时make-anonymous-func,lambda 中的列表被识别为列表文字,并且未正确评估。

(funcall (make-anonymous-func '(message "foo")) "bar")
; ===> (when (not (string-empty-p arg)) (message "foo") (message arg))
Run Code Online (Sandbox Code Playgroud)

有没有一些好的方法可以让它发挥作用?
谢谢。

lisp elisp

2
推荐指数
1
解决办法
158
查看次数

标签 统计

elisp ×1

lisp ×1