我现在正努力学习Lisp,作为CS1课程的补充,因为课程对我来说太慢了我学会了“实用的通用语言”,到目前为止,这本书已经成为一本很好的书,但是我很难找到一些有用的例子例如,如果我将以下文件加载到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
。我在这里做错什么了?
最佳答案
没什么,阿法特:
$sbcl公司
这是SBCL 1.0.34.0。。。
[[逐字粘贴在上面的代码中,然后:]]
*(addRecord(制作cd“白色专辑”“披头士”5t)
(:TITLE“白色专辑”:艺人“披头士”:评分5:ripted T)
*(选择(艺人选择“披头士”))
(:TITLE“白色专辑”:艺人“披头士”:评分5:ripted T)