Note: this is the stubbed version of module Queue. Try to figure out
how to fill in all parts of this file marked
undefined
.
CIS 5520 students should be able to access this code through
github. Eventually, the
completed version will be available.
In class exercise: Purely Functional Queues
Today's technical challenge is to implement a persistent queue data structure.
> module Queue where
You should use QuickCheck to test this module. If you want to use additional
library operations to complete this exercise, you may import them here. (For
example, our solution uses at least one function from the Data.Maybe
library.)
> import Test.QuickCheck
- Define an interface for a purely functional Queue (FIFO). Your interface
must (at least) define some data structure, called
Q
, include a representation of an empty queue (queue), a way to add an element to the end of the queue (enq) and a way to remove the element at the beginning of the queue (deq), if the queue is nonempty. The queue must be polymorphic over the type of elements that it stores. You may include additional operations in your interface, if you wish.
> -- replace these definitions with something more appropriate
> data Q = Q
> empty :: Q
> enq :: Q
> deq :: Q
- Now define some properties that your queue should satisfy. (Note: you may want to add additional operations to your queue interface to help with stating these properties.)
- Implement your interface. Hint: the simplest implementation uses a single list. However, you can define a more efficient version using two lists.
> empty = undefined
> enq = undefined
> deq = undefined
- Make an
Arbitrary
instance, including definitions for botharbitrary
andshrink
- Run your tests.