want to make stack

Discussion of Common Lisp

want to make stack

Postby bhupi » Fri Apr 20, 2012 1:56 am

;; IN THIS PROGRAM I WANT TO CHANGE REMOVE-FRONT FUNCTION TO RETURN ;;VALUES IN REVERSE ORDER LIKE
;;IF I ENTER a c b it returns a then c then b but i want that b must be returned first ;;then c then a
(defstruct q
(key #'identity)
(last nil)
(elements nil))



(defun make-empty-queue () (make-q))

(defun empty-queue? (q)
"Are there no elements in the queue?"
(= (length (q-elements q)) 0))

(defun queue-front (q)
"Return the element at the front of the queue."
(elt (q-elements q) 0))

(defun remove-front (q)
"Remove the element from the front of the queue and return it."
(if (listp (q-elements q))
(pop (q-elements q))
))



(defun enqueue-at-front (q items)
"Add a list of items to the front of the queue."
(setf (q-elements q) (nconc items (q-elements q))))
bhupi
 
Posts: 7
Joined: Fri Apr 13, 2012 4:46 am

Re: want to make stack

Postby nuntius » Fri Apr 20, 2012 3:35 pm

Please provide attribution when you reuse code from others.
http://aima.cs.berkeley.edu/lisp/utilities/queue.lisp

CL already has PUSH and POP for stack manipulations. If a normal project, these are the default choice. In a homework assignment, both should be avoided; here, you want tools like CONS, CAR, CDR, and SETF. Plus there's the built-in REVERSE function.

Are you working on SICP exercise 2.18? There are a few elegant recursive solutions to the problem, as well as the usual iterative solutions.
User avatar
nuntius
 
Posts: 497
Joined: Sat Aug 09, 2008 10:44 am
Location: Burlington, MA


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 3 guests

cron