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