Tag Archives: obscure commands

The less-familiar parts of Lisp for beginners — dribble

The next obscure command we’ll talk (briefly) about is dribble.  This is nothing more than an output capturing function that allows you to keep a record of your interaction with the Lisp environment.  You should usually not be using this.  You should be using SLIME under Emacs to do your Lisp development, and you can keep a much more helpful record using those tools.  I will note that dribble does not play well with SLIME, at least in my setup, which uses SBCL v1.1.14, Emacs 24.1.1, and SLIME from late January, 2013.

The less-familiar parts of Lisp for beginners — do-symbols

Now we move on to a set of three macros, do-all-symbols, do-external-symbols, and do-symbols.  These macros allow the code to iterate over a list of symbols as they are known to the Lisp image.  The programmer might use these to inspect the execution environment and possibly call code in optional libraries, but there are usually easier ways to achieve that.  One place that I have seen it used is similar to the use of weak symbols in the C++ linker.  A piece of code can use do-external-symbols to detect collisions and import other symbols into its namespace only when there is no existing function/variable with the same name.  The assumption is that there might be some very basic interface that is optionally extended in another file.  The extended package can pick out those functions and variables from the basic package that are not re-implemented.  Naturally, this requires very tight cooperation between the two interfaces, but I’ve seen it done.

What do these functions do?  Well, do-symbols iterates over all symbols available to a package (or the current package if none is supplied).  Generally, that’s all the symbols that the package creates, plus all the external symbols in other packages, including all the symbols in the Lisp language itself.

The do-external-symbols macro iterates over all symbols exported by the package (or the current package if none is supplied).  This is the one you’re most likely to use.

The do-all-symbols macro iterates over all symbols in all packages.  It’s a symbol dump of the Lisp image itself.

Note that do-symbols and do-all-symbols may list some symbols more than once, so if your goal is to visit each symbol exactly once, you’ll have to take steps to skip the duplicates.

The less-familiar parts of Lisp for beginners — deposit-field

Now we move on to deposit-field.  A cursory introduction to Lisp is unlikely to have discussed this function.  To begin, I’d suggest that you review the earlier article on byte.  The deposit-field function is used to remove a bitfield from one integer, SRC, and insert it into another integer, DST, returning the result.  How is this different from dpb, whose effects were shown in that article on byte?  In dpb, the byte specifier acts only to window DST.  That is, if the byte specifier is 5 bits wide, then the lowest 5 bits of SRC are used, regardless of the position of the byte specifier.  In deposit-field, SRC and DST are both windowed by the byte specifier, so it is possible to insert bits other than the least-significant bits of SRC.

This sort of bitfield manipulation is not used very frequently.  It’s essential when writing code to certain binary protocols, very useful for device drivers, network stacks, and things like that, but less commonly used in other applications.

The less-familiar parts of Lisp for beginners — delete-package

Our next obscure Lisp command for newcomers arriving from C++ is delete-package.  To begin, I’ll discuss what a package behaves like from the perspective of C++.

A package is a bit like a C++ namespace, but with a few extra twists.  In C++, a namespace is essentially a name-mangling tool.  It allows the programmer to create symbols without worrying too much about symbol name collisions.  In Lisp, a package has this quality, but also other properties.  First of all, a Lisp package is a real obect in the language, not simply a compiler directive for symbol naming.  The package is, very broadly, a symbol container.  Secondly, packages can inherit from other packages, and can therefore inherit symbols or override them as necessary.  Don’t confuse a package with a class, though, as you can define many classes or no classes in a package, and there isn’t an equivalent to polymorphism in the syntax of package symbol resolution, the way there is with class hierarchies and generic functions.

So, what does delete-package do?  Well, perhaps unsurprisingly, it deletes the package.  The result is that the classes and functions defined in the package are no longer visible in the Lisp environment.  Once again, the need for this is due to the fact that Lisp programs run in a Lisp environment, and not as self-contained things that are compiled and then executed.

For the newcomer, when might you need delete-package?  Most obviously, during development.  If you’re working in a package, and you’ve defined some generic functions, only to decide later that you need to change the interface of a generic function and the methods that implement it, your Lisp instance will complain when  you try to re-load the package, because redefining the generic function will bring it into conflict with the implemented methods.  By calling delete-package before reloading the modified file, you delete the changes that the package made to the Lisp system, and so can safely load the file.  Other cases where you might need to delete the package before loading it include when you’ve got a defconstant on a list (see the earlier discussion there), or when you’ve used defvar in the package.  Really, any time when the state of the package might modify the effect of loading the package again.