Page 1 of 1

this queue program is not running showing error

Posted: Wed Apr 18, 2012 10:05 am
by bhupi

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

Re: this queue program is not running showing error

Posted: Wed Apr 18, 2012 9:06 pm
by bhupi
still this program is not working

plss help i need it by tomorrow morning

Re: this queue program is not running showing error

Posted: Thu Apr 19, 2012 3:58 am
by saulgoode
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).

Re: this queue program is not running showing error

Posted: Thu Apr 19, 2012 4:58 am
by nuntius
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.

Re: this queue program is not running showing error

Posted: Thu Apr 19, 2012 5:10 am
by nuntius
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)))))))