2

I'm trying to learn Lisp now, as a supplement to my CS1 course because the class was moving too slow for me. I picked up "Practical Common Lisp," which so far has turned out to be a great book, but I'm having some trouble getting some examples to work. For instance, if I load the following file into the 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))) 

And query the 'database' using (select (artistSelector "The Beatles")), even if I do indeed have an entry in the database where :artist is equal to "The Beatles", the function returns NIL.

What am I doing incorrectly here?

1
  • note that EQUAL is case sensitive Commented Sep 3, 2010 at 18:20

2 Answers 2

4

Nothing, AFAICT:

 $ 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)) 
Sign up to request clarification or add additional context in comments.

4 Comments

And of course if you never call addRecord then indeed, select will return NIL.
Okay, so the problem seems to occur when I add records via the addCDs function, but not when I call addRecord directly.
I figured it out. When using the addCDs function, you don't enter the strings in enclosed in quotation marks.
Heh. I completely missed (addCDs). I looked for select, as in the question, and then looked back to see what I needed to call that. Oops!
1
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)) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.