我开始通过实用的常见LISP工作,第一个练习是编写一个简单的数据库。我在Cygwin上使用GNU CLISP 2.48(2009-07-28)。
我几次与这本书进行了比较的此代码,并没有像该书所说的那样产生输出
(defun make-cd (title artist rating ripped)
(list :title title :artist artist :rating rating :ripped))
(defvar *db* nil)
(defun add-record (cd) (push cd *db*))
(add-record (make-cd "Roses" "Kathy Mattea" 7 t))
(add-record (make-cd "Fly" "Dixie Chicks" 8 t))
(add-record (make-cd "Home" "Dixie Chicks" 9 t))
(defun dump-db ()
(dolist (cd *db*)
(format t "~{~a:~10t~a~%~}~%" cd)))
(dump-db)
我得到
TITLE: Home
ARTIST: Dixie Chicks
RATING: 9
RIPPED:
*** - There are not enough arguments left for this format directive.
Current point in control string:
"~{~a:~10t~a~%~}~%"
|
我不了解format
或LISP,足以进行故障排除。这本书说我应该在数据库中获取所有记录的列表。出了什么问题?
首先,让我们看一下(make-cd)的返回:
[12]> (make-cd "Home" "Dixie Chicks" 9 t)
(:TITLE "Home" :ARTIST "Dixie Chicks" :RATING 9 :RIPPED)
您不包括:ripped
的值!更改(make-cd)为:
(defun make-cd (title artist rating ripped)
(list :title title :artist artist :rating rating :ripped ripped))
注意:ripped
之后的ripped
。
如果您在clisp中使用编译器,它会告诉您什么问题:
[1]> (defun make-cd (title artist rating ripped)
(list :title title :artist artist :rating rating :ripped))
MAKE-CD
[2]> (compile 'make-cd)
WARNING: in MAKE-CD : variable RIPPED is not used.
Misspelled or missing IGNORE declaration?
MAKE-CD ;
1 ;
NIL
不使用变量RIPPED
。
格式指令~{...~}
是一种迭代构造,其相应的参数预计为列表。此外,在这种情况下,由于~a
的两次出现,每次迭代将消耗两个项目,因此列表中的项目总数均匀。但是您提供了奇数的项目。