s c h e m a t i c s : c o o k b o o k

/ DataStructures? / Cookbook.FunctionalQueue

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Functional Queue

A queue is a data structure supporting the following operations: empty?, insert, remove and first.

Problem

The problem is to get implement both insert and remove in time O(1) without mutation.

Solution

Represent the queue as two lists. The first list holds elements in the front of the queue, and the second elements in the back of the queue (in reverse order). Insert can now be implemented simply by consing the new element to the back of the queue. If the front is non-empty, remove simple returns the first element of the front, otherwise the elements from the back are moved to the front. This gives insert and remove amortized time O(1).

(module queue mzscheme
  (provide empty empty? insert remove first)
  
  (define-struct queue (front back) (make-inspector))
  
  (define empty (make-queue '() '()))
  
  ; empty? : queue -> boolean
  ;  is the queue Q empty?
  (define (empty? Q)
    (and (null? (queue-front Q))
         (null? (queue-back Q))))
  
  ; insert : queue elm -> queue
  ;  return a new queue holding both the elements
  ;  from the old queue Q and the new element x
  (define (insert Q x)  
    (make-queue (queue-front Q) 
                (cons x (queue-back Q))))

  ; remove : queue -> (values queue elm)
  ;  return two values, the first a queue with the same elements
  ;  as the queue Q except for the element in front of the queue
  ;  the second a value is the front element of Q
  (define (remove Q)
    (cond
      [(and (null? (queue-front Q)) (null? (queue-back Q)))
       (error "remove: The queue is empty")]
      [(null? (queue-front Q))
       (remove (make-queue (reverse (queue-back Q)) '()))]
      [else
       (values (car (queue-front Q)) 
               (make-queue (cdr (queue-front Q)) (queue-back Q)))]))

  ; first : queue -> element
  ;  return the first element in the non-empty queue Q
  (define (first Q)
    (cond
      [(empty? Q)
       (error "first: The queue is empty")]
      [(null? (queue-front Q))
       (first (make-queue (reverse (queue-back Q)) '()))]
      [else
       (car (queue-front Q))]))
  )

> (require queue)
> (define Q empty)
> (let* ([Q (insert Q 1)]
         [Q (insert Q 2)]
         [Q (insert Q 3)])
    (let*-values ([(x Q) (remove Q)]
                  [(y Q) (remove Q)]
                  [(z Q) (remove Q)])
      (list x y z)))
(list 1 2 3)

> (print-struct #t)
> (remove (insert (insert (insert empty 
                                  1)
                          2)
                  3))
1
#(struct:queue (2 3) ())

Discussion

One advantage of a functional datastructures is that they can be used by multithreaded programs (such as web servlets) without problems.

-- JensAxelSoegaard - 17 Apr 2004

CookbookForm
TopicType: Recipe
ParentTopic: DataStructures?
Next Topic:

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest