2020-05-27 11:27:10 UTC
(defstruct a-cell data next)
(head (make-a-cell) :type a-cell)
(tail (make-a-cell) :type a-cell))
It is meant as a model of singly-linked lists. When creating a-list with
(let ((lst (make-a-list))) lst)
the lst represents an empty list. head and tail are dummy cell's just
A first list item (let ((x (make-a-cell))) x) would be consed into the
list such that head.next points to x and x.next points to tail.
After creating an empty list with (make-a-list) head.next should point
to tail and tail.next should points to head (via their next slots).
head ---------> tail
But after (make-a-list) head.next and tail.next are null.
When instead using
(defclass a-cell ()
((data :initarg :data :initform nil :accessor data)
(next :initarg :next :initform nil :accessor next)))
(defclass a-list ()
((head :initform (make-instance 'cell) :accessor head)
(tail :initform (make-instance 'cell) :accessor tail)))
the initialisation as intended could be done using
(defmethod initialize-instance :after ((l a-list) &key)
(setf (next (head l)) (tail l))
(setf (next (tail l)) (head l)))
Is this kind of initialisation as in initialize-instance where I use
slots to initialise slots also possible for the struct version above?