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



我有这样的代码:

(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)不是一个有效的Common 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

最新更新