The Fifo structure provides a functional queue data structure,
which are implemented as a pair of stacks (lists) representing the
front and rear of the queue. Single-threaded enqueuing and
dequeuing operations will have amortized constant time.
Synopsis
signature FIFO
structure Fifo :> FIFO
Interface
type 'a fifo
exception Dequeue
val empty : 'a fifo
val isEmpty : 'a fifo -> bool
val enqueue : 'a fifo * 'a -> 'a fifo
val dequeue : 'a fifo -> 'a fifo * 'a
val next : 'a fifo -> ('a * 'a fifo) option
val delete : ('a fifo * ('a -> bool)) -> 'a fifo
val head : 'a fifo -> 'a
val peek : 'a fifo -> 'a option
val length : 'a fifo -> int
val contents : 'a fifo -> 'a list
val app : ('a -> unit) -> 'a fifo -> unit
val map : ('a -> 'b) -> 'a fifo -> 'b fifo
val foldl : ('a * 'b -> 'b) -> 'b -> 'a fifo -> 'b
val foldr : ('a * 'b -> 'b) -> 'b -> 'a fifo -> 'b
Description
type 'a fifo-
The type constructor for functional queues.
exception Dequeue-
This exceptions is raised when the
dequeuefunction is applied to an empty queue. val empty : 'a fifo-
The empty queue.
val isEmpty : 'a fifo -> bool-
ifEmpty qreturns true if the queue is empty. val enqueue : 'a fifo * 'a -> 'a fifo-
enqueue (q, x)returns a queue withxadded to the end. val dequeue : 'a fifo -> 'a fifo * 'a-
dequeue qreturns a pair(q', x), wherexwas the first element inqandq'is the queue withxremoved. This function raises theDequeueexception if it is called on an empty queue. val next : 'a fifo -> ('a * 'a fifo) option-
next qreturnsSOME(q', x), wherexwas the first element inqandq'is the queue withxremoved, orNONEifqis empty. val delete : ('a fifo * ('a -> bool)) -> 'a fifo-
delete (q, pred)removes those items fromqfor which the functionpredreturnstrueand returns the resulting queue. val head : 'a fifo -> 'a-
head qreturns the first element ofqor raises the exceptionDequeueifqis empty. val peek : 'a fifo -> 'a option-
peek qreturnsSOME x, wherexis the first element ofq, orNONEifqis empty. val length : 'a fifo -> int-
length qreturns the number of elements in the queue. val contents : 'a fifo -> 'a list-
contents qreturns the contents ofqas a list. val app : ('a -> unit) -> 'a fifo -> unit-
app f qapplies the functionfto the elements ofq. This expression is equivalent toList.app f (contents q) val map : ('a -> 'b) -> 'a fifo -> 'b fifo-
map f qreturns the queue that results from mapping the functionfacross the elements of the queue. val foldl : ('a * 'b -> 'b) -> 'b -> 'a fifo -> 'b-
foldl f init qfolds the functionfover the elements ofqfrom front to back. This expression is equivalent toList.foldl f init (contents q) val foldr : ('a * 'b -> 'b) -> 'b -> 'a fifo -> 'b-
foldr f init qfolds the functionfover the elements ofqfrom back to front. This expression is equivalent toList.foldr f init (contents q)