Функция, ошибочно возвращающая ноль - PullRequest
2 голосов
/ 03 сентября 2010

Я сейчас пытаюсь выучить Лисп в качестве дополнения к моему курсу CS1, потому что класс двигался слишком медленно для меня.Я взял «Практический Common Lisp», который до сих пор оказался отличной книгой, но у меня возникли некоторые проблемы с получением примеров для работы.Например, если я загружаю следующий файл в REPL:

;;;; Created on 2010-09-01 19:44:03

(defun makeCD (title artist rating ripped)
  (list :title title :artist artist :rating rating :ripped ripped))

(defvar *db* nil)

(defun addRecord (cd) 
  (push cd *db*))

(defun dumpDB ()
  (dolist (cd *db*)
    (format t "~{~a:~10t~a~%~}~%" cd)))

(defun promptRead (prompt)
    (format *query-io* "~a: " prompt)
    (force-output *query-io*)
    (read-line *query-io*))

(defun promptForCD ()
    (makeCD
        (promptRead "Title")
        (promptRead "Artist")
        (or (parse-integer (promptRead "Rating") :junk-allowed t) 0)
        (y-or-n-p "Ripped [y/n]: ")))

(defun addCDs ()
    (loop (addRecord (promptForCD))
        (if (not (y-or-n-p "Another? [y/n]: ")) (return))))

(defun saveDB (fileName)
    (with-open-file (out fileName
            :direction :output
            :if-exists :supersede)
        (with-standard-io-syntax 
            (print *db* out))))

(defun loadDB (fileName)
    (with-open-file (in fileName)
        (with-standard-io-syntax
            (setf *db* (read in)))))

(defun select (selectorFn)
    (remove-if-not selectorFn *db*))

(defun artistSelector (artist)
    #'(lambda (cd) (equal (getf cd :artist) artist)))

и запрашиваю «базу данных», используя (select (artistSelector "The Beatles")), даже если у меня действительно есть запись в базе данных, где :artist равенв "The Beatles" функция возвращает NIL.

Что я здесь делаю неправильно?

Ответы [ 2 ]

4 голосов
/ 03 сентября 2010

Ничего, АФАКТ:

$ sbcl
This is SBCL 1.0.34.0...

[[pasted in code above verbatim, then:]]

* (addRecord (makeCD "White Album" "The Beatles" 5 t))

((:TITLE "White Album" :ARTIST "The Beatles" :RATING 5 :RIPPED T))
* (select (artistSelector "The Beatles"))

((:TITLE "White Album" :ARTIST "The Beatles" :RATING 5 :RIPPED T))
1 голос
/ 03 сентября 2010
CL-USER 18 > (addcds)
Title: Black Album
Artist: Prince
Rating: 10
Title: White Album
Artist: The Beatles
Rating: 10
NIL

CL-USER 19 > (select (artistSelector "The Beatles"))
((:TITLE "White Album" :ARTIST "The Beatles" :RATING 10 :RIPPED T))
...