2012-11-05 59 views
3

我開始通過Practical Common LISP工作,第一個練習是編寫一個simple database。我在cygwin上使用GNU CLISP 2.48(2009-07-28)。麻煩格式化第一練習從實用常見LISP

此代碼,我已經對書幾次相比,不產生輸出的方式,書上說應該

(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以便排除故障。這本書說我應該得到數據庫中所有記錄的清單。出了什麼問題?

回答

4

首先,讓我們來看看在返回從(使-CD):

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

您還沒有包括:ripped值!更改(請-CD)到:

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

注意ripped:ripped後。

+0

呃,我真傻!我花了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這兩次出現,每次迭代將消耗兩個項目,因此列表中項目的總數預計會是偶數。然而,你提供了奇數個項目。