okasaki

Okasaki's Purely Functional Data Structures
git clone git://git.jtobin.io/okasaki.git
Log | Files | Refs | LICENSE

Queue.hs (722B)


      1 {-# OPTIONS_GHC -Wall #-}
      2 
      3 module Queue where
      4 
      5 import Prelude hiding (head, tail, reverse)
      6 import qualified Prelude as Prelude (reverse)
      7 
      8 data Queue a = Queue {
      9     forward :: [a]
     10   , reverse :: [a]
     11   } deriving Show
     12 
     13 head :: Queue a -> Maybe a
     14 head (Queue [] _)    = Nothing
     15 head (Queue (h:_) _) = Just h
     16 
     17 empty :: Queue a
     18 empty = Queue [] []
     19 
     20 isEmpty :: Queue a -> Bool
     21 isEmpty (Queue [] []) = True
     22 isEmpty _ = False
     23 
     24 checkf :: Queue a -> Queue a
     25 checkf (Queue [] r) = Queue (Prelude.reverse r) []
     26 checkf q = q
     27 
     28 snoc :: Queue a -> a -> Queue a
     29 snoc (Queue f r) e = checkf (Queue f (e : r))
     30 
     31 tail :: Queue a -> Queue a
     32 tail (Queue (_:t) r) = checkf (Queue t r)
     33 tail q = q
     34 
     35 test :: Queue Int
     36 test = Queue [1,2,3] [6,5,4]
     37