[ create a new paste ] login | about

Link: http://codepad.org/22B4rkGy    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Nov 1:
; queues

(define (make-queue) (list (list)))

(define (enqueue q x) (cons (car q) (cons x (cdr q))))

(define (head q)
  (if (pair? (car q))
      (caar q)
      (if (pair? (cdr q))
          (car (reverse (cdr q)))
          (error 'head "empty"))))

(define (tail q)
  (if (pair? (car q))
      (cons (cdar q) (cdr q))
      (if (pair? (cdr q))
          (cons (cdr (reverse (cdr q))) (list))
          (error 'tail "empty"))))

(define (empty? q) (and (null? (car q)) (null? (cdr q))))

(define q (make-queue))
(set! q (enqueue q 1))
(set! q (enqueue q 2))
(display (empty? q)) (newline)
(display (head q)) (newline)
(set! q (tail q))
(display (head q)) (newline)
(set! q (enqueue q 3))
(set! q (tail q))
(display (head q)) (newline)
(set! q (tail q))
(display (empty? q)) (newline)


Output:
1
2
3
4
5
#f
1
2
3
#t


Create a new paste based on this one


Comments: