~ubuntu-branches/ubuntu/utopic/haskell-uulib/utopic

« back to all changes in this revision

Viewing changes to src/UU/DData/Seq.hs

  • Committer: Bazaar Package Importer
  • Author(s): Joachim Breitner
  • Date: 2011-06-04 00:54:17 UTC
  • mfrom: (9.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110604005417-4uxlka1134z0ig1w
Tags: 0.9.13-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
--------------------------------------------------------------------------------
2
 
{-| Module      :  Seq
3
 
    Copyright   :  (c) Daan Leijen 2002
4
 
    License     :  BSD-style
5
 
 
6
 
    Maintainer  :  daan@cs.uu.nl
7
 
    Stability   :  provisional
8
 
    Portability :  portable
9
 
 
10
 
  An implementation of John Hughes's efficient catenable sequence type. A lazy sequence
11
 
  @Seq a@ can be concatenated in /O(1)/ time. After
12
 
  construction, the sequence in converted in /O(n)/ time into a list.
13
 
-}
14
 
---------------------------------------------------------------------------------}
15
 
module UU.DData.Seq( -- * Type
16
 
            Seq
17
 
            -- * Operators
18
 
          , (<>)
19
 
 
20
 
            -- * Construction
21
 
          , empty
22
 
          , single
23
 
          , cons
24
 
          , append
25
 
 
26
 
            -- * Conversion
27
 
          , toList
28
 
          , fromList
29
 
          ) where
30
 
 
31
 
 
32
 
{--------------------------------------------------------------------
33
 
  Operators
34
 
--------------------------------------------------------------------}
35
 
infixr 5 <>
36
 
 
37
 
(<>) :: Seq a -> Seq a -> Seq a
38
 
s <> t
39
 
  = append s t
40
 
 
41
 
{--------------------------------------------------------------------
42
 
  Type
43
 
--------------------------------------------------------------------}
44
 
newtype Seq a = Seq ([a] -> [a])
45
 
 
46
 
{--------------------------------------------------------------------
47
 
  Construction
48
 
--------------------------------------------------------------------}
49
 
empty :: Seq a
50
 
empty
51
 
  = Seq (\ts -> ts)
52
 
 
53
 
single :: a -> Seq a
54
 
single x
55
 
  = Seq (\ts -> x:ts)
56
 
 
57
 
cons :: a -> Seq a -> Seq a
58
 
cons x (Seq f)
59
 
  = Seq (\ts -> x:f ts)
60
 
 
61
 
append :: Seq a -> Seq a -> Seq a
62
 
append (Seq f) (Seq g)
63
 
  = Seq (\ts -> f (g ts))
64
 
 
65
 
 
66
 
{--------------------------------------------------------------------
67
 
  Conversion
68
 
--------------------------------------------------------------------}
69
 
toList :: Seq a -> [a]
70
 
toList (Seq f)
71
 
  = f []
72
 
 
73
 
fromList :: [a] -> Seq a
74
 
fromList xs
75
 
  = Seq (\ts -> xs++ts)
76
 
 
77
 
 
78
 
 
79
 
 
80
 
 
81
 
 
82
 
 
83