4
votes

I want to write some Lisp code like this

(defstruct board
    (size 7)
    (matrix (make-array (list size size))
    (red-stones 0)
    (black-stones 0))

in order to define a structure that represents a game's board.

I want to be able to create a new board with make-board that will create the matrix on the fly with the given dimension (size x size), so that I don't have to call make-board from another function that makes the matrix.

However, when I load that code into the clisp interpreter and try to make a new board (with make-board), I get an error message telling me that "size" has no value.

Is there any way to use the fields of the structure in the very definition of the structure?

Or should I do this?

(defstruct board
    size
    matrix
    (red-stones 0)
    (black-stones 0))

(defun create-board (size)
    (make-board :size size :matrix (make-array (list size size))) )

Really, I don't like having both make-board and create-board available, because that may lead to programming mistakes.

2
I would suggest that you familiarize yourself with the CLOS which is essentially the object/class system for Common Lisp. With your board defined as a class, you would be able to specify methods via (defmethod create-board ((type-of thing))).. That is, various methods for working with the same class dependent upon what sort of information the user-form supplies. If you do go down this rabbit hole, I suggest this introduction to CLOS, as well as inclusion of :CL-MOP the Closer To MOP system for metaclasses, ql:quickload-able. - miercoledi

2 Answers

2
votes

You could use a boa constructor:

(defstruct (board
    (:constructor make-board (&optional (size 7)
                              &aux (matrix (make-array (list size size))))))
  (size)
  (matrix)
  (red-stones 0)
  (black-stones 0))

CLHS documentation for defstruct and BOA lambda lists.

1
votes

Really, I don't like having both make-board and create-board available, because that may lead to programming mistakes.

There's a fair point, but then, by having size and matrix both available can lead to programming mistakes, too.

It doesn't take O(n) to measure the size of an array, so I'd just eliminate the size slot altogether. If you commonly want that value for a board, it's easy to make a simple wrapper function.

On the chance you want a more general solution to "some of my slots are determined by some of my other slots", Kenny Tilton's Cells project, though it doesn't appear to be terribly active these days.

Finally, I'd use defclass rather than defstruct unless you have a reason not to.