LISP动态函数?
我试图教自己一些LISP,虽然我理解其中的大部分,但我很难掌握eval函数。我知道它已经为我们做了,而且它不适合使用(我听说是这样),但是我该如何制作一个只添加了一些内容的函数呢 到目前为止,我一直在努力/思考LISP动态函数?,lisp,common-lisp,Lisp,Common Lisp,我试图教自己一些LISP,虽然我理解其中的大部分,但我很难掌握eval函数。我知道它已经为我们做了,而且它不适合使用(我听说是这样),但是我该如何制作一个只添加了一些内容的函数呢 到目前为止,我一直在努力/思考 (setf input-prompt "Enter addition epression: ") (setf output-prompt "The value is: ") (defun prompt-for-input (msg) (format t msg)) (defun
(setf input-prompt "Enter addition epression: ")
(setf output-prompt "The value is: ")
(defun prompt-for-input (msg)
(format t msg))
(defun sum (expression)
(format t "Summing ~d and ~d.~%" x y)
(+ x y))
(defun add ()
(prompt-for-input input-prompt)
(let ((expression (read)))
((sum (expression)))
(add)))
我不知道该怎么办,谢谢你的帮助
(setf input-prompt "Enter addition expression: ")
(setf output-prompt "The value is: ")
(defun prompt-for-input (msg)
(format t msg)
(finish-output))
(defun sum (expression)
(let ((x (second expression))
(y (third expression)))
(format t "~%Summing ~d and ~d.~%" x y)
(+ x y)))
(defun add ()
(prompt-for-input input-prompt)
(sum (read)))
运行它:
CL-USER > (add)
Enter addition expression: (+ 1 2)
Summing 1 and 2.
3
LISP是如何计算“第二个”和“第三个”表达式的?
(第二个表达式)
是列表中的第二个元素表达式
。您的代码没有编译,但我知道了它的要点。因此,如果我想创建其他函数,比如sum并将“add”更改为通用表达式持有者,我会检查使用subseq将其发送到哪个函数吗?@Nogg:为什么它不编译?它为我编译。你想做什么?听起来您希望根据所读取的表达式调用不同的函数,而不使用eval
。这基本上是编写一个小型解释器,这是大多数Lisp书籍(LittleSchemer等)中都涉及到的内容。但如果你能更清楚地描述你的问题,这会有所帮助。