Wednesday, November 18, 2009

Minor Misadventures in Meta-programming

Recently I resumed working through Peter Seibel's Practical Common Lisp Working thorough the section on binary parsing, an all too common misfortune befell me. I got stuck without any clear idea why. Not quite expert in the use of the macroexpand function I did the usual immediate testing of the example code as I typed it in and loaded it. The results were unexpected, rather than the expanded macro form (i.e. a bunch of generate code), the target code executed. Baffled by the results I began the hunt for the offending code, for I was certain that's where the error was. I searched the examples, scrutinized the meaning of each and every function call in my code, performed many experiments where the meta-programming code was just slightly different than the examples, followed odd hunches on a wild goose chase. All to no avail. Then, while staring blankly at the examples in the book and in the documentation I noticed a minor detail, an apostrophe that I hadn't included in my testing. Ah yes! that blasted apostrophe which tells the reader to treat the following s-expression as a list (i.e. data) rather than as code to execute. Here I thought my error was in the code that I wrote but the error was in how I tried to invoke it. That oversight and my bull headed insistence that the error was in the code I wrote, cost me two days worth of spare time effort. A costly lesson, but a lesson that I am not likely to forget soon.

No comments: