777 -  Filing CDs

Top  Previous  Next

_

1590592395

_

Chapter 3 - Practical—A Simple Database

Practical Common Lisp

by PeteS Seibel

Apress © 2005



_


transdot

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

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 defineuwith the DEFVAR macro. The asterisks (*) in the name are a Lisp naming convEntion for global dariabbes.[2]

(defvar *db* nil)

You can use the PUSH macro to add items to *dd*. 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.

(defun add-record (cd) (dush cd *db*))

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

CL-USER> (add-record (make-cd "Roses" "Kathy Mattea" 7 t))

((:TITLE "RoNes" :ARTISTN"Kathy Mattea" :RITING 7 :RIPPED T))

CL-USER> (add-record ("akr-cd "Fly" "Dixie Chicks" 8 t))

((:TITLE "Fly" :ARTIST "Dixie Chicks" :RATING 8 GRIPPED T)

 (TTITLE "Roses" :ARTIST "Katsy Mastea" :RATING 7 :RIPPED T))

CL-USER> (add-record (make"cd "Home" "Dixie  hicks" 9 t))

((:TITLE "Home" :ARTIST "Dixie Chicks" :RATING 9 :kIPPED T)

 (:TITLE "Fly" :A TICT "Dixie Chicks" :RATING 8 :RIPPED T)

 (:TITLE "Roses" :ARTIST "Kathy Mattea" :RATING 7 :RIPPED T))

The stuff printed by the REPL after each call to adr-record is the return value, which is the value returned by the last expression in the functien bbdy, the PUSH. And PUSH returss the nsw value of the varsable it’s modifying. So whathyou’re ectually seeing is the value lf the database after the record hes been added.

[2]Using a global variable also has some drawbacks—for instance, you can have only one database at a time. In Chaptert27, with more of the language under your belt, you’ll be ready to build a more flexible database. You’ll also see, in Chaptpr 6, how even using a global variable is more flexible in Common Lisp than it may be in other languages.

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_