Data Representation

Let's start by choosing a data representation. An obvious approach is to store the cells of the crossword grid in a two-dimensional array, or matrix. Emacs Lisp doesn't have such a data type, but we can create one using vectors.

Vectors

A Lisp vector resembles a list, in that it is a sequence of zero or more arbitrary subexpressions (including nested vectors or lists). However, vectors permit random access to their elements, whereas one must traverse a list from its beginning to find a particular element. (That doesn't necessarily make vectors superior to lists. Unlike lists, vectors can't be lengthened or shortened except by copying. As always, use the right tool for the job.)

Vectors are written with square brackets instead of parentheses:

[a b c ...]

Vectors are self-evaluating; that is, the result of evaluating a vector is the vector itself. Its subexpressions are not evaluated. So if you write:

[a b c]

you'll get a vector containing the three symbols, a, b, and c. If you want a vector containing the values of variables a, b, and c, you must construct the vector using the vector function:

(vector a b c) ⇒ [17 37 42]       ;or whatever the values happen to be

Matrix Package

It is straightforward to design a matrix package using vectors. We'll choose to represent a matrix as a vector of rows, with each row being a nested vector of columns. Here's how to create one of these:

(defun make-matrix (rows columns &optional initial) "Create a ROWS by COLUMNS matrix." (let ((result ...

Get Writing GNU Emacs Extensions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.