Common lisp 如何在运行时编译s表达式,然后在Common Lisp中执行它

Common lisp 如何在运行时编译s表达式,然后在Common Lisp中执行它,common-lisp,runtime-compilation,Common Lisp,Runtime Compilation,我正在编写一个生成程序的程序(遗传编程)。我在运行时构建了一个s表达式,今天我使用eval,如下所示: (defvar *program* '(+ x 1)) (defvar x 10) (eval *program*) ;; returns 11 对multiple x进行计算,我想在运行时将s表达式编译成一个函数,然后对multiple x调用它以提高性能 我不知道怎么做,我会感谢你的帮助。以下是我所拥有的: ;;;; This is not working code (defmacro

我正在编写一个生成程序的程序(遗传编程)。我在运行时构建了一个s表达式,今天我使用eval,如下所示:

(defvar *program* '(+ x 1))
(defvar x 10)
(eval *program*) ;; returns 11
对multiple x进行计算,我想在运行时将s表达式编译成一个函数,然后对multiple x调用它以提高性能

我不知道怎么做,我会感谢你的帮助。以下是我所拥有的:

;;;; This is not working code
(defmacro compile-program (args &body body)
  (compile nil `(lambda (,@args)
                  (declare (ignorable ,@args))
                  (progn ,@body))))

(funcall (compile-program (x) *program*) 10) ;; returns (+ X 1)
(funcall (compile-program (x) (+ x 1)) 10) ;; returns 11
编辑: 对@RainerJoswig I的Thx进行了以下修改,并且有效:

;;;; Working Code
(defvar *program* '(+ x 1))

(defun compile-program (args program)
  (compile nil `(lambda ,args
                   (declare (ignorable ,@args))
                    ,program)))

(funcall (compile-program '(x) *program*) 10) ;; returns 11
;;;; Working Code
(defvar *program* '(+ x 1))

(defun compile-program (args program)
  (compile nil `(lambda ,args
                   (declare (ignorable ,@args))
                    ,program)))

(funcall (compile-program '(x) *program*) 10) ;; returns 11

Thx对@RainerJoswig和@coredump I进行了以下修改,并且有效:

;;;; Working Code
(defvar *program* '(+ x 1))

(defun compile-program (args program)
  (compile nil `(lambda ,args
                   (declare (ignorable ,@args))
                    ,program)))

(funcall (compile-program '(x) *program*) 10) ;; returns 11
;;;; Working Code
(defvar *program* '(+ x 1))

(defun compile-program (args program)
  (compile nil `(lambda ,args
                   (declare (ignorable ,@args))
                    ,program)))

(funcall (compile-program '(x) *program*) 10) ;; returns 11

编译程序不应该是宏。您的宏还有一个参数程序,您不使用它。使用未定义的变量体。无论如何,请从函数开始。@RainerJoswig Thx以获取您的建议。我修正了未定义的body变量,这是编辑问题时的一个错误。但它仍然是一个宏,为什么?这看起来更好。您还可以删除
progn
。不需要。
(,@args)
,args