当我运行我的代码时,它说我的 fill-lib 函数中有一个未绑定(bind)的变量,但我不知道哪个变量未绑定(bind)或为什么未绑定(bind)。
(defstruct book
(title nil)
(author nil)
(genre nil))
(setf b (make-book))
(setf lib ())
(defun fill-lib ()
(setq count 1)
(with-open-file (s-stream "/Users/David/Desktop/Books.txt"
:direction :input)
(loop
(cond (> count 1) (return (princ "Library filled")))
(setf b (make-book))
(setf (book-title b) (read-line s-stream))
(setf (book-author b) (read-line s-stream))
(setf (book-genre b) (read-line s-stream))
(setf lib (cons b lib))
(setq count (+ count 1)))))
最佳答案
cond
special for 具有抽象语法 (cond (<test> [<form> ...])...)
.也就是说,每个“语句”或“测试”都需要是一个列表,以测试形式开头。如果测试形式不是零(这本质上是 Common Lisp 的“真”),则结果形式在“隐式程序”(基本上是“代码块”)中进行评估。如果没有后续形式,则返回值将是测试形式的值。如果为 nil(Common Lisp 中的“false”),则下一个测试以相同的方式进行评估。
你有 (cond (> count 1) (return (princ "Library filled")))
, 这是一个 cond
形式有两种形式,第一种是(> count 1)
,这将被解释为“如果变量 >
非零,首先评估 count
,然后评估 1
并让最后的结果成为 cond 形式的结果”。
如果您使用 when
,它(可能)会被清除而不是 cond
在这里,因为 (when (> count 1) (return (princ "Library filled")))
会做我只能假设你想做的事(打印“Library filled”并从函数返回该字符串)。
关于variables - Lisp 中的未绑定(bind)变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38453038/