Filing CDs

A single record, however, does not a database make. You need some larger construct to hold the records. Again, for simplicity’s sake, a list seems like a good choice. Also for simplicity you can use a global variable, *db*, which you can define with the **DEFVAR** macro. The asterisks (*) in the name are a Lisp naming convention for global variables.2

  1. (defvar *db* nil)

You can use the **PUSH** macro to add items to *db*. But it’s probably a good idea to abstract things a tiny bit, so you should define a function add-record that adds a record to the database.

  1. (defun add-record (cd) (push cd *db*))

Now you can use add-record and make-cd together to add CDs to the database.

  1. CL-USER> (add-record (make-cd "Roses" "Kathy Mattea" 7 t))
  2. ((:TITLE "Roses" :ARTIST "Kathy Mattea" :RATING 7 :RIPPED T))
  3. CL-USER> (add-record (make-cd "Fly" "Dixie Chicks" 8 t))
  4. ((:TITLE "Fly" :ARTIST "Dixie Chicks" :RATING 8 :RIPPED T)
  5. (:TITLE "Roses" :ARTIST "Kathy Mattea" :RATING 7 :RIPPED T))
  6. CL-USER> (add-record (make-cd "Home" "Dixie Chicks" 9 t))
  7. ((:TITLE "Home" :ARTIST "Dixie Chicks" :RATING 9 :RIPPED T)
  8. (:TITLE "Fly" :ARTIST "Dixie Chicks" :RATING 8 :RIPPED T)
  9. (:TITLE "Roses" :ARTIST "Kathy Mattea" :RATING 7 :RIPPED T))

The stuff printed by the REPL after each call to add-record is the return value, which is the value returned by the last expression in the function body, the **PUSH**. And **PUSH** returns the new value of the variable it’s modifying. So what you’re actually seeing is the value of the database after the record has been added.