Function 如何执行存储在变量中的Lisp程序?

Function 如何执行存储在变量中的Lisp程序?,function,lisp,common-lisp,clisp,Function,Lisp,Common Lisp,Clisp,我有以下代码: (setf prg '(+ 1 n)) ; define a very simple program (print prg) ; print the program 我需要添加更多的代码,以便在执行上述代码时,它应该将n设置为1并执行 存储在变量prg中的程序。我想您应该这样做: (setf prg (lambda (n) + 1 n)) ; define a very simple program (print (funcall prg 1)) ; print t

我有以下代码:

(setf prg '(+ 1 n)) ; define a very simple program
(print prg) ; print the program
我需要添加更多的代码,以便在执行上述代码时,它应该将n设置为1并执行
存储在变量prg中的程序。

我想您应该这样做:

(setf prg (lambda (n) + 1 n)) ; define a very simple program
(print (funcall prg 1))       ; print the program
在您的示例中:
(+1 n)
不是有效的公共Lisp程序

编辑:如果您想使用变量绑定,还可以声明一个变量:

(setf prg '(+ 1 n)) ; define a Common Lisp expression
(defparameter n 1)  ; bind a variable to the value 1
(print (eval prg))  ; evaluate the Common Lisp expression
> 2

我想你应该这样做:

(setf prg (lambda (n) + 1 n)) ; define a very simple program
(print (funcall prg 1))       ; print the program
在您的示例中:
(+1 n)
不是有效的公共Lisp程序

编辑:如果您想使用变量绑定,还可以声明一个变量:

(setf prg '(+ 1 n)) ; define a Common Lisp expression
(defparameter n 1)  ; bind a variable to the value 1
(print (eval prg))  ; evaluate the Common Lisp expression
> 2

Prg是一个包含3个值的列表,您需要在n绑定到0的环境中对其进行评估。可以使用defvar将n全局绑定到0。为什么不使用一个函数呢?Prg是一个由3个值组成的列表,您需要在n绑定到0的环境中对其求值。可以使用defvar将n全局绑定到0。为什么不改用函数呢?