Tag Archives: obscure commands

The less-familiar parts of Lisp for beginners — find-symbol

The next function we visit is find-symbol.  This is used to determine whether a particular package has a named symbol, and whether that symbol is internal, external, or inherited from another package.

I would recommend that you review the for thorough discussion of what a Lisp package is, under its own article.  A package in Lisp has qualities that a C++ programmer would look at as something like a cross between namespaces and classes.

The find-symbol function allows the user to query a package to discover whether a particular name is a symbol in the package, and if so, whether it is internal to the package, external to the package, or derived from an ancestor package.  This can be helpful, for example, in designing work-arounds for different versions of a library, by probing for functions not present in all versions of the package.

The less-familiar parts of Lisp for beginners — find-restart

We now move on to find-restart.  This is not quite the same as the various find- methods we’ve been very briefly discussing recently.  There is a bit more to say about this function.

The find-restart function provides some information about the current lexical environment.  That is, it allows a form to get insight into the nature of the forms that enclose it.  Now, before talking about this, you might find it helpful to review the earlier series of posts on exception handling in Lisp.

Broadly, you can say that find-restart returns to the caller the restart object that would be invoked to handle a particular condition, or nil if there is no such restart.  In C++ terms, this is a way, at run time, for a function to ask whether there is an enclosing catch-er for a particular thrown exception.

The less-familiar parts of Lisp for beginners — find-class

Another feature of lisp that the casual new user probably hasn’t encountered is find-class.  This accessor retrieves the class definition of the symbol.  I’ve talked a bit about the class and polymorphism model in Lisp before, see here and here.

As we’ve mentioned throughout this series, a Lisp program is put together somewhat differently than a C++ program.  In Lisp, we start with a Lisp image, and then, by loading forms, we augment the image with definitions of functions and data structures.  So, you might ask, what actually happens when a defclass form is loaded into the image?  Throughout this series I’ve been sticking to C++ nomenclature.  The “class” is the abstract definition that tells the system what is involved in creating objects.  The “object” is a single instance of the class.  But what happens when a defclass form is loaded into a Lisp image?  Well, it creates a new object.  Not an object of the class you’re defining, it creates an object of type standard-class.  That object is tied to a symbol, the name of the class itself.  The make-instance function actually uses find-class or its equivalent to retrieve the object associated with the name of the object your instantiating, and then uses that object to construct the instance.  So, find-class retrieves the object that defines the class, just as fdefinition retrieves the actual function bound to the name passed.

You’re not likely to find very many uses for this accessor.  It can be used in the implementations of defclass and make-instance, and there might be some situations where you want your code to examine a symbol to determine whether it’s a class, a structure, or something else, but these are quite esoteric applications.