开发者

How to define structures in Lisp using parameters in the definition

开发者 https://www.devze.com 2023-01-29 01:26 出处:网络
I want t开发者_如何学Goo write some Lisp code like this (defstruct board (size 7) (matrix (make-array (list size size))

I want t开发者_如何学Goo 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消