Lisp中的未绑定变量 [英] Unbound variable in Lisp
问题描述
当我运行代码时,它说我的fill-lib函数中有一个未绑定的变量,但是我不知道哪个变量是未绑定的或为什么.
When I run my code it says that there is an unbound variable in my fill-lib function, but I have no clue what variable is unbound or why.
(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
具有抽象语法(cond (<test> [<form> ...])...)
.也就是说,每个声明"或测试"都必须是一个列表,从测试表格开始.如果测试形式为非零(本质上是真"的Common Lisp),则随后的形式将以隐式形式"(基本上是代码块")进行评估.如果没有后续表格,则返回值将是测试表格的值.如果为零(Common Lisp为"false"),则以相同的方式评估下一个测试.
The cond
special for has the abstract syntax (cond (<test> [<form> ...])...)
. That is, each "statement" or "test" needs to be a list, starting with a test form. If the test form is non-nil (that's essentially Common Lisp for "true"), the consequent forms are evaluated in an "implicit progn" (basically "code block"). If there are no consequent forms, the return value would be the value of the test form. If it is nil (Common Lisp for "false"), the next test is evaluated in the same manner.
您有(cond (> count 1) (return (princ "Library filled")))
,这是具有两种形式的cond
形式,第一种是(> count 1)
,这将被解释为如果变量>
为非nil,则首先求值count
,然后计算1
并将最后的结果作为cond形式的结果."
You have (cond (> count 1) (return (princ "Library filled")))
, this is a cond
form with two forms, the first being (> count 1)
, which will be interpreted as "if the variable >
is non-nil, first evaluate count
, then evaluate 1
and let the result of that last be the result of the cond form".
如果您在这里使用when
而不是cond
,它将被(可能)清除,因为(when (> count 1) (return (princ "Library filled")))
会执行我只能假设您要执行的操作(打印"Library filld",并从功能).
It would (probably) be cleared if you used when
instead of cond
here, since (when (> count 1) (return (princ "Library filled")))
would do what I can only assume you want to do (print "Library filled" and return that string from the function).
这篇关于Lisp中的未绑定变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!