Skip to content
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

Open
minghu6 opened this issue Aug 9, 2020 · 3 comments
Open

How about add stack (similiar with queue) #70

minghu6 opened this issue Aug 9, 2020 · 3 comments

Comments

@minghu6
Copy link

minghu6 commented Aug 9, 2020

We use the queue like a FIFO stateful list
However, it lacks the stack used like a LIFO stateful list.

@minghu6
Copy link
Author

minghu6 commented Aug 9, 2020

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)))

@ruricolist
Copy link
Owner

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 deq and undeq you can already use queues as stacks -- that should probably be documented.)

ruricolist added a commit that referenced this issue Aug 9, 2020
@minghu6
Copy link
Author

minghu6 commented Aug 10, 2020

I find that list as primarty type, pass by value as function arguments.
For example:

(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.
For queue using as a stack, deq for pop and undeq for push is a little confused and complicated.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants