omniyo
omniyo

Reputation: 340

Nested structures in Lisp

I'm starting with Lisp and I need to know if it is possible to use nested structures. Here is my try:

(defstruct casilla i j)
(defstruct tablero caballo reina t1 t2)

(defparameter *estado-inicial*
  (make-tablero :caballo (make-casilla :i 1 :j 1)
                :reina   (make-casilla :i 5 :j 4)
                :t1      (make-casilla :i 3 :j 5)
                :t2      (make-casilla :i 4 :j 5)))

And if I have to access to the field i:

(defun es-estado-final (estado)
  (and (= (caballo-casilla-i estado) 3)
       (= (caballo-casilla-j estado) 1)))

Is that right? It seems it isn't because caballo-casilla-i is undefined. Thanks in advance.

Upvotes: 3

Views: 500

Answers (1)

Vsevolod Dyomkin
Vsevolod Dyomkin

Reputation: 9451

For structs your Lisp environment created for you automatically the accessors tablero-caballo and casilla-i. To combine them you need to use (casilla-i (tablero-caballo estado)). Obviously, you can roll out your own accessor function:

(defun caballo-casilla-i (estado)
  (casilla-i (tablero-caballo estado)))

Also you can (declaim (inline caballo-casilla-i)) to not waste additional function calls.

Upvotes: 5

Related Questions