如何执行存储在变量中的 Lisp 程序?
How do I execute a Lisp program which has been stored in a variable?
我有这个代码:
(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
我有这个代码:
(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