Вопрос

I'm beginning to work through Practical Common LISP and the first exercise is to write a simple database. I'm using GNU CLISP 2.48 (2009-07-28) on cygwin.

This code, which I've compared against the book several times, doesn't produce output the way the book says it should

(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)

I get

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~%~}~%"
                  |

I don't understand format or LISP well enough to being to troubleshoot. The book says I should be getting a list of all the records in the database. What has gone wrong?

Это было полезно?

Решение

First, let's look at the return from (make-cd):

[12]> (make-cd "Home" "Dixie Chicks" 9 t)
(:TITLE "Home" :ARTIST "Dixie Chicks" :RATING 9 :RIPPED)

You aren't including a value for :ripped! Change (make-cd) to:

(defun make-cd (title artist rating ripped)
  (list :title title :artist artist :rating rating :ripped ripped))

Note the ripped after :ripped.

Другие советы

If you use the compiler in CLISP, it tells you what is wrong:

[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

The variable RIPPED is not used.

The format directive ~{...~} is an iterative construct, and its corresponding argument is expected to be a list. Furthermore in this case, because of the two occurrences of ~a, each iteration will consume two items, so the total number of items in the list is expected to be even. Yet you provided it with an odd number of items.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top