this queue program is not running showing error

Discussion of Common Lisp
Post Reply
bhupi
Posts: 7
Joined: Fri Apr 13, 2012 4:46 am

this queue program is not running showing error

Post by bhupi » Wed Apr 18, 2012 10:05 am

Code: Select all

(defun make-queue
    (lambda ()
      (let ((front '()) (back '()))
        (lambda (cmd . data)
          (define exchange
           (lambda ()
              (set! front (reverse back))
              (set! back '())))
          (case cmd
           ((push) (push (car data) back))
           ((pop) (or (pop front)
                       (begin
                         (exchange)
                         (pop front))))
            ((peek) (unless (pair? front)
                     (exchange))
                      (car front))
            ((show) (format #t "~s\n" (append front (reverse back))))
            ((fb) (format #t "front: ~s, back: ~s\n" front back))
            (else (error "Illegal cmd to queue object" cmd)))))))
Last edited by bhupi on Wed Apr 18, 2012 11:33 pm, edited 2 times in total.

bhupi
Posts: 7
Joined: Fri Apr 13, 2012 4:46 am

Re: this queue program is not running showing error

Post by bhupi » Wed Apr 18, 2012 9:06 pm

still this program is not working

plss help i need it by tomorrow morning

saulgoode
Posts: 45
Joined: Tue Dec 14, 2010 1:39 am

Re: this queue program is not running showing error

Post by saulgoode » Thu Apr 19, 2012 3:58 am

Which dialect of Scheme are you running? (I don't know of any that use 'defun'.)

Also, you need to define all of your procedures push, pop, peek, show, and fb somewhere (so that they evaluate properly during invocation).

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: this queue program is not running showing error

Post by nuntius » Thu Apr 19, 2012 4:58 am

My bad. This first post was in the moderation queue. From a quick glance, my tired eyes saw Scheme so I moved the topic. Bhupi's other post is clearly CL code.

In CL,

Code: Select all

(defun make-queue
  (lambda ()
...
should probably look like

Code: Select all

(defun make-queue ()
  ...
But the use of "set!" looks like Scheme (use "setf" in CL); so I'm still not sure what dialect is intended.

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: this queue program is not running showing error

Post by nuntius » Thu Apr 19, 2012 5:10 am

The following is "valid" CL. It approximates the basic form of the original post using CL idioms, but it almost certainly does not work.

Code: Select all

(defun make-queue ()
  (let ((front '()) (back '()))
    (lambda (cmd data)
      (flet
	  ((exchange ()
		     (setf front (reverse back))
		     (setf back '())))
	(case cmd
	  ((:push) (push (car data) back))
	  ((:pop) (unless (pop front)
		    (exchange)
		    (pop front)))
	  ((:peek) (unless (consp front)
		     (exchange))
	   (car front))
	  ((:show) (format t "~s\n" (append front (reverse back))))
	  ((:fb) (format t "front: ~s, back: ~s\n" front back))
	  (t (error "Illegal cmd to queue object" cmd)))))))

Post Reply