The First Snag at Chapter 2
Plunging through PCL Chapter 2. It’s an exposition chapter to kind of show off what lisp can do by making a simple database for a music collection using property lists. Pretty cool, right?
And halfway through I hit a snag. Yesterday everything worked fine. Today I added some of load and save features, started up the REPL again and BOOM! the function to add records no longer worked. Not the new functions, mind you, but the one that already worked. Frustration and Head Scratching abounded.
I realized what the difference was. When I restarted the REPL, obviously the previous definitions were cleared. This should not have been a problem, because I reloaded the file with all my work. But here is the catch. The book builds up the program piece by piece, so that by the time you have all the major functions built, the global *db* is already defined. But that value gets wiped if you restart. So when the code tried to push the list onto *db* it landed me in the debugger. Apparently one cannot instantiate variables that way, at least in lisp.
Added (defvar *db*) to the file and it works fine now, even after restarting the REPL.
This leaves me with two (for now) questions:
- Would setf have been a better choice?
- More generally, I dislike the idea of defining globals, so what might be a better way to achieve the same goals?