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.
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.