| 1 |
-- | Que.hs |
|---|
| 2 |
-- A simple O(1) queue. |
|---|
| 3 |
-- |
|---|
| 4 |
-- Copytight (C) 2007, 2008 Serguey Zefirov |
|---|
| 5 |
-- |
|---|
| 6 |
-- This program is free software; you can redistribute it and/or modify |
|---|
| 7 |
-- it under the terms of the GNU General Public License as published by |
|---|
| 8 |
-- the Free Software Foundation; either version 3, or (at your option) |
|---|
| 9 |
-- any later version. |
|---|
| 10 |
-- See file COPYING or visit http://www.gnu.org/licenses for details. |
|---|
| 11 |
|
|---|
| 12 |
-- I wrote this because broken compatibility in Data.Seq library module between |
|---|
| 13 |
-- my two home computers with ghc 6.4 and ghc 6.6+. |
|---|
| 14 |
-- |
|---|
| 15 |
|
|---|
| 16 |
module Que( |
|---|
| 17 |
Que(..) |
|---|
| 18 |
,empty |
|---|
| 19 |
,fromList |
|---|
| 20 |
,toList |
|---|
| 21 |
,head |
|---|
| 22 |
,size |
|---|
| 23 |
,insert |
|---|
| 24 |
,append |
|---|
| 25 |
,appends |
|---|
| 26 |
,take |
|---|
| 27 |
) where |
|---|
| 28 |
|
|---|
| 29 |
import Prelude (id,(.),(++),($),reverse,length,otherwise,Ord(..),Num(..),Maybe(..),Show(..)) |
|---|
| 30 |
|
|---|
| 31 |
data Que a = Que [a] [a] |
|---|
| 32 |
|
|---|
| 33 |
empty = Que [] [] |
|---|
| 34 |
|
|---|
| 35 |
head (Que (x:xs) ys) = Just (x,Que xs ys) |
|---|
| 36 |
head (Que [] []) = Nothing |
|---|
| 37 |
head (Que _ ys) = let (x:xs) = reverse ys in Just (x,Que xs []) |
|---|
| 38 |
|
|---|
| 39 |
insert x (Que xs ys) = Que (x:xs) ys |
|---|
| 40 |
|
|---|
| 41 |
append y (Que xs ys) = Que xs (y:ys) |
|---|
| 42 |
appends [] = id |
|---|
| 43 |
appends (x:xs) = appends xs . append x |
|---|
| 44 |
|
|---|
| 45 |
size (Que xs ys) = length xs + length ys |
|---|
| 46 |
|
|---|
| 47 |
fromList xs = Que xs [] |
|---|
| 48 |
|
|---|
| 49 |
toList (Que xs ys) = xs++reverse ys |
|---|
| 50 |
|
|---|
| 51 |
instance Show a => Show (Que a) where |
|---|
| 52 |
show q = "fromList "++show (toList q) |
|---|
| 53 |
|
|---|
| 54 |
take n q |
|---|
| 55 |
| n > 0, Just (x,q') <- head q = let (l,q'') = take (n-1) q' in (x:l,q'') |
|---|
| 56 |
| otherwise = ([],q) |
|---|