麻烦格式化实践共同LISP的第一次练习

时间:2012-11-05 04:17:47

标签: lisp common-lisp practical-common-lisp

我开始通过Practical Common LISP工作,第一个练习就是写一个simple database。我在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是否足以排除故障。这本书说我应该得到数据库中所有记录的列表。出了什么问题?

3 个答案:

答案 0 :(得分:4)

首先,让我们看一下(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

答案 1 :(得分:4)

如果你在LISP中使用编译器,它会告诉你出了什么问题:

[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

答案 2 :(得分:1)

格式指令~{...~}是一个迭代构造,其相应的参数应该是一个列表。此外,在这种情况下,由于两次出现~a,每次迭代将消耗两个项目,因此列表中的项目总数应该是偶数。然而,你提供了奇数项目。