Tag Archives: lisp

Concluding thoughts on the “less familiar parts of Lisp” series

I’ve now finished my brief walk-through of the functions, macros, and special forms described in the Common Lisp Hyper Spec.  Note that these posts only scratch the surface of the CLHS, there is a lot more useful and informative material there, such as standard class hierarchies (including error type hierarchies), definitions, examples, and collected discussions related to clarification of the standard.

This project was primarily for my own benefit, to better understand those features of the language that I hadn’t run into in my use of Common Lisp.  The language definition is, with a few exceptions for convenience, quite orthogonal.  That is, the standard tends not to define so many functions that you can do a particular thing several different ways.  Given this, knowing that I only recognized perhaps half of the commands in the language indicated that I was missing out on a lot of things that Lisp could do, and this series of posts was intended to allow me to familiarize myself with the other parts of the language.  To that end, I made sure not to skip any entry merely because I wasn’t clear on its definition.  Some experimentation, searching the web, and checking through source code of packages and Lisp implementations allowed me to grasp those functions.

So, for me, this was a useful project, and I’d recommend doing something like it to a newcomer who is trying to get a better grasp of Common Lisp.  As with any language, understanding your tools is immensely important, not just to avoid reinventing features with your own special bugs, but also to help in the layout and planning of your code before you start writing.

There’s one more thing a newcomer to Lisp should do, and that is really get a firm grasp of macros.  We know that C and C++ discourage C-preprocessor macros, for valid reasons, but Lisp macros are a very different species, and once you understand their power, it opens up entire new realms of possibilities.  There are those who claim that a good understanding of Lisp macros even helps when programming in other languages, and perhaps that’s true, at least for some.  I think an understanding of Lisp macros helps the programmer to think of the algorithms themselves in an abstract way, just as algorithms help the programmer think of data in an abstract way, and that that abstraction leads to a more complete understanding of the problem.

The less familiar parts of Lisp for beginners — summary T, U, V, W:

I’ve written a few posts about features of the Lisp language that a newcomer arriving from C++ might not have encountered.  I’m going through them alphabetically, so here is a summary page of the functions beginning with the letters T, U, V, and W:

the

throw

typecase

unuse-package

unwind-protect

update-instance-for-different-class

update-instance-for-redefined-class

upgraded-array-element-type

upgraded-complex-part-type

vector-pop, vector-push, and vector-push-extend

with-accessors

with-compilation-unit

with-package-iterator

with-slots

with-standard-io-syntax

The less familiar parts of Lisp for beginners — summary S

I’ve written a few posts about features of the Lisp language that a newcomer arriving from C++ might not have encountered.  I’m going through them alphabetically, so here is a summary page of the functions beginning with the letter S:

scale-float and related functions

search

set-macro-character

set-pprint-dispatch

shadow

shadowing-import

shared-initialize

shiftf

signal

slot-boundp

slot-exists-p

slot-makunbound

slot-missing

slot-unbound

slot-value

special

special-operator-p

stream-element-type

stream-external-format

sublis and nsublis

subtypep

sxhash

symbol-function

symbol-macrolet

symbol-package

The less-familiar parts of Lisp for beginners — with-standard-io-syntax

The next feature we’ll discuss is the with-standard-io-syntax macro.  This is probably underused in many contexts, and its absence can lead to some mysterious behaviour in certain circumstances.

You’ll recall that there are dynamic variables that modify the behaviour of the Lisp writer, as we discussed in several articles related to printing.  We also saw variables that modify the behaviour of the Lisp reader, the most obvious of which is the *read-table* dynamic variable.

When writing a function that writes and/or reads, if that function might be called from an unknown context it is strongly recommended that with-standard-io-syntax is used to wrap those input/output operations.  This macro sets the control variables to a set of well-defined defaults.  The programmer can always embed further let forms to tweak these as necessary, but it’s important to start in a known state.

One can imagine the potential for weirdness if a library that manipulates data files with write were called with the *print-base* variable set to, say, 8.