Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top