首页 > 解决方案 > 如何执行已存储在变量中的 Lisp 程序?

问题描述

我有这个代码:

(setf prg '(+ 1 n)) ; define a very simple program
(print prg) ; print the program

我需要添加更多代码,以便在执行上述代码时,它应该将 n 设置为 1 并执行存储在变量 prg 中的程序。

标签: functionlispcommon-lispclisp

解决方案


我想你想这样做:

(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

推荐阅读