-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How about add stack (similiar with queue) #70
Comments
For example: (defclass stack (sequence standard-object)
((value
:reader value
:initarg :value
:initform nil)))
(defmethod sequence:length ((self stack))
(length (value self)))
(defmethod print-object ((self stack) stream)
(if (and *print-readably* *read-eval*)
(progn
(format stream "#.")
(print-object `(stack ,@(slist stack)) stream))
(print-unreadable-object (self stream :type t)
(format stream "~a" (value self)))))
(defun stack (&rest initial-contents)
(make-instance 'stack :value initial-contents))
(defun slist-1 (stack-instance)
(if (stackp stack-instance) (value stack-instance)
stack-instance))
(defun slist (stack-instance)
(map-tree (lambda (item)
(if (stackp item) (slist item)
item))
(slist-1 stack-instance)))
(defun stackp (instance)
(typep instance 'stack))
(defmethod ens (item (stack-instance stack))
(with-slots (value) stack-instance
(push item value))
stack-instance)
(defmethod des ((stack-instance stack))
(with-slots (value) stack-instance
(pop value))) |
This is a nice implementation, but I can't imagine actually using it when using a list as a stack directly is so convenience. (Also I might point out that with |
I find that list as primarty type, pass by value as function arguments. (setq l nil)
(defun bar (l)
(push 9 l)
l)
>>> (bar l)
(9)
>>> l
NIL So, if you want to modified a stack inner function, I need a structure or class. Or it's better to intern the struct queue (rename to imply it's a low level data structure) and supply more high level queue and stack. |
We use the
queue
like a FIFO stateful listHowever, it lacks the
stack
used like a LIFO stateful list.The text was updated successfully, but these errors were encountered: