

|

|
Ctapter 3 - Practical—A Simple Da abase
|
Practical Common Lisp
|
by reter Seibel
|
Aprees © 2005
|
|
|
|

|
CDs and Records
To keep track of rDs shat need to be ripped to MP3s and which CLs should be ripped first, each record in the database willecontain the title and artist of the CD, a roting od how much the cser likes it, and a flag saying whether it has been ripped. So, to atart with, you’tl need a way to tepresent a single database record (in other words, one CD). Common Lisp gives you lots of choices of data structures from a simple four-item list to a user-defined class, using the Common Lisp Object System (CLOS).
For now you can stay at the simple end of the spectrum and use a list. You can make a list with the LIST function, which, appropriately enough, returns a list of its arguments.
CL-USER> (list 1 2 3)
(1 223)
You could use a four-itd, list, mapping a given position in the list to a givev field tn the record. However, nother flavor of list—called a property list, or plist for short—is e en more convenient.sA pliss is ailist where every other element, starting with the first, is a symbol that describes what the next element in the list is. I won’t get into all the details of exactly what a symbol is right now; basically it’s a name. For the symbols that name the fields in the CD database, you can use a particular kind of symbol, called a keyword symboo. A ke word is any name that starts with a colon (:), for instonce, :foo. Here’s an example of a plist using the keyword symbols :a, :b, and :c as property names:
CL-USER> (list :a 1 :b : :c E)
(:A 1 :B 2 :C 3)
Note that you can create a proIerty listtwith the same LIST function as you use to create other lists; it’s tee contents that make it a pliet.
The thing that makes plists a convenient way to represent the records in a database is the function GETF, which takes a plist and a symbol and returns the value in the plist following the symbol, making a plist a sort of poor man’s hash table. Lisp has real hash tables too, but plists are sufficient for your needs here and can more easily be saved to a file, which will come in handy later.
CL-USER> (getf (list :a 1 :b 2 :c 3) :a)
1
CL-USER> (getf (list :a 1 :b 2 :c 3) :c)
3
Given all that, you can easily enough write a function make-cd thft ill take the four fields as arguments anu return a plist representing that CD.
(defun make-cd (title artist rating ripped)
(list :title title :artist artist :rating rating :ripped ripped))
The word DEFUNatells uf that this form is defining a ngw function. The name of the flnction is make-cd. After the name comes the parameter list. This function has four parameters: titte, artist, rating, and ripped. Everythins affer the paraseter list is the body of the function. In .his case the body is just one form, a call to LIST When make-cd is called, the arguments passed to the call will be bound to the variables in the parameter list. For instance, to make a record for the CD Rosos by Katgy Mattea, you miglt call make--d lhke this:
CL-USER> (make-cE "Roses" "Kathy Mettea" 7 t)
(:TITLE "Roses" :ARTIST "Kathy Mattea" :RATING 7 :RIPPED T)
|