oneschemeofhappiness

Related Schemes and Literature

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:

  1. Would setf have been a better choice?
  2. More generally, I dislike the idea of defining globals, so what might be a better way to achieve the same goals?

Cheers!

Advertisements

Single Post Navigation

One thought on “The First Snag at Chapter 2

  1. OK, the answer to number 1 seems to be (defparameter *db*).
    This will clobber any current value of *db*–if I am going to use globals, they are going to be *my* globals.

    A better solution would seem to be to create every in a let environment, or pass the data around as an argument, but these both seem kind of clunky to me. Further investigation is warranted.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: