实用Common Lisp第一项练习格式化问题

3

我开始学习《实用的Common 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的理解不够深入,无法排除故障。书上说我应该得到数据库中所有记录的列表。出了什么问题?
3个回答

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

哎呀,我真是太傻了!我花了15分钟交叉检查那个格式字符串。 - user151841

4
如果您使用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 没有被使用。

1

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接