[code 1] shows a implementation of queue. The function enqueue! returns a queue in that the obj is added at the last of the queue. The function dequeue!removes the first item from the queue and return the first item.
[code 1]
(define (make-queue)
(cons '() '()))
(define (enqueue! queue obj)
(let ((lobj (cons obj '())))
(if (null? (car queue))
(begin
(set-car! queue lobj)
(set-cdr! queue lobj))
(begin
(set-cdr! (cdr queue) lobj)
(set-cdr! queue lobj)))
(car queue)))
(define (dequeue! queue)
(let ((obj (car (car queue))))
(set-car! queue (cdr (car queue)))
obj))
(define q (make-queue)) ;Value: q (enqueue! q 'a) ;Value 12: (a) (enqueue! q 'b) ;Value 12: (a b) (enqueue! q 'c) ;Value 12: (a b c) (dequeue! q) ;Value: a q ;Value 13: ((b c) c)
实际上
(make-queue q 'a)
后输出q为:
((a) a)
(enqueue! q 'b)
(a b)
> q
((a b) b)
http://www.shido.info/lisp/scheme_asg_e.html