Copyright  (c) Ross Paterson 2005 (c) Louis Wasserman 2009 (c) Bertram Felgenhauer, David Feuer, Ross Paterson, and Milan Straka 2014 

License  BSDstyle 
Maintainer  libraries@haskell.org 
Portability  portable 
Safe Haskell  Trustworthy 
Language  Haskell98 
WARNING
This module is considered internal.
The Package Versioning Policy does not apply.
This contents of this module may change in any way whatsoever and without any warning between minor versions of this package.
Authors importing this module are expected to track development closely.
Description
General purpose finite sequences. Apart from being finite and having strict operations, sequences also differ from lists in supporting a wider variety of operations efficiently.
An amortized running time is given for each operation, with \( n \) referring to the length of the sequence and \( i \) being the integral index used by some operations. These bounds hold even in a persistent (shared) setting.
The implementation uses 23 finger trees annotated with sizes, as described in section 4.2 of
 Ralf Hinze and Ross Paterson, "Finger trees: a simple generalpurpose data structure", Journal of Functional Programming 16:2 (2006) pp 197217. http://staff.city.ac.uk/~ross/papers/FingerTree.html
Note: Many of these operations have the same names as similar
operations on lists in the Prelude. The ambiguity may be resolved
using either qualification or the hiding
clause.
Warning: The size of a Seq
must not exceed maxBound::Int
. Violation
of this condition is not detected and if the size limit is exceeded, the
behaviour of the sequence is undefined. This is unlikely to occur in most
applications, but some care may be required when using ><
, <*>
, *>
, or
>>
, particularly repeatedly and particularly in combination with
replicate
or fromFunction
.
Since: 0.5.9
 newtype Elem a = Elem {
 getElem :: a
 data FingerTree a
 data Node a
 data Digit a
 class Sized a where
 class MaybeForce a
 newtype Seq a = Seq (FingerTree (Elem a))
 newtype State s a = State {
 runState :: s > (s, a)
 execState :: State s a > s > a
 foldDigit :: (b > b > b) > (a > b) > Digit a > b
 foldNode :: (b > b > b) > (a > b) > Node a > b
 foldWithIndexDigit :: Sized a => (b > b > b) > (Int > a > b) > Int > Digit a > b
 foldWithIndexNode :: Sized a => (m > m > m) > (Int > a > m) > Int > Node a > m
 empty :: Seq a
 singleton :: a > Seq a
 (<) :: a > Seq a > Seq a
 (>) :: Seq a > a > Seq a
 (><) :: Seq a > Seq a > Seq a
 fromList :: [a] > Seq a
 fromFunction :: Int > (Int > a) > Seq a
 fromArray :: Ix i => Array i a > Seq a
 replicate :: Int > a > Seq a
 replicateA :: Applicative f => Int > f a > f (Seq a)
 replicateM :: Applicative m => Int > m a > m (Seq a)
 cycleTaking :: Int > Seq a > Seq a
 iterateN :: Int > (a > a) > a > Seq a
 unfoldr :: (b > Maybe (a, b)) > b > Seq a
 unfoldl :: (b > Maybe (b, a)) > b > Seq a
 null :: Seq a > Bool
 length :: Seq a > Int
 data ViewL a
 viewl :: Seq a > ViewL a
 data ViewR a
 viewr :: Seq a > ViewR a
 scanl :: (a > b > a) > a > Seq b > Seq a
 scanl1 :: (a > a > a) > Seq a > Seq a
 scanr :: (a > b > b) > b > Seq a > Seq b
 scanr1 :: (a > a > a) > Seq a > Seq a
 tails :: Seq a > Seq (Seq a)
 inits :: Seq a > Seq (Seq a)
 chunksOf :: Int > Seq a > Seq (Seq a)
 takeWhileL :: (a > Bool) > Seq a > Seq a
 takeWhileR :: (a > Bool) > Seq a > Seq a
 dropWhileL :: (a > Bool) > Seq a > Seq a
 dropWhileR :: (a > Bool) > Seq a > Seq a
 spanl :: (a > Bool) > Seq a > (Seq a, Seq a)
 spanr :: (a > Bool) > Seq a > (Seq a, Seq a)
 breakl :: (a > Bool) > Seq a > (Seq a, Seq a)
 breakr :: (a > Bool) > Seq a > (Seq a, Seq a)
 partition :: (a > Bool) > Seq a > (Seq a, Seq a)
 filter :: (a > Bool) > Seq a > Seq a
 lookup :: Int > Seq a > Maybe a
 (!?) :: Seq a > Int > Maybe a
 index :: Seq a > Int > a
 adjust :: (a > a) > Int > Seq a > Seq a
 adjust' :: forall a. (a > a) > Int > Seq a > Seq a
 update :: Int > a > Seq a > Seq a
 take :: Int > Seq a > Seq a
 drop :: Int > Seq a > Seq a
 insertAt :: Int > a > Seq a > Seq a
 deleteAt :: Int > Seq a > Seq a
 splitAt :: Int > Seq a > (Seq a, Seq a)
 elemIndexL :: Eq a => a > Seq a > Maybe Int
 elemIndicesL :: Eq a => a > Seq a > [Int]
 elemIndexR :: Eq a => a > Seq a > Maybe Int
 elemIndicesR :: Eq a => a > Seq a > [Int]
 findIndexL :: (a > Bool) > Seq a > Maybe Int
 findIndicesL :: (a > Bool) > Seq a > [Int]
 findIndexR :: (a > Bool) > Seq a > Maybe Int
 findIndicesR :: (a > Bool) > Seq a > [Int]
 foldMapWithIndex :: Monoid m => (Int > a > m) > Seq a > m
 foldlWithIndex :: (b > Int > a > b) > b > Seq a > b
 foldrWithIndex :: (Int > a > b > b) > b > Seq a > b
 mapWithIndex :: (Int > a > b) > Seq a > Seq b
 traverseWithIndex :: Applicative f => (Int > a > f b) > Seq a > f (Seq b)
 reverse :: Seq a > Seq a
 intersperse :: a > Seq a > Seq a
 liftA2Seq :: (a > b > c) > Seq a > Seq b > Seq c
 zip :: Seq a > Seq b > Seq (a, b)
 zipWith :: (a > b > c) > Seq a > Seq b > Seq c
 zip3 :: Seq a > Seq b > Seq c > Seq (a, b, c)
 zipWith3 :: (a > b > c > d) > Seq a > Seq b > Seq c > Seq d
 zip4 :: Seq a > Seq b > Seq c > Seq d > Seq (a, b, c, d)
 zipWith4 :: (a > b > c > d > e) > Seq a > Seq b > Seq c > Seq d > Seq e
 unzip :: Seq (a, b) > (Seq a, Seq b)
 unzipWith :: (a > (b, c)) > Seq a > (Seq b, Seq c)
Documentation
data FingerTree a Source #
Functor FingerTree Source #  
Foldable FingerTree Source #  
Traversable FingerTree Source #  
NFData a => NFData (FingerTree a) Source #  
Sized a => Sized (FingerTree a) Source #  
Generalpurpose finite sequences.
Seq (FingerTree (Elem a)) 
Monad Seq Source #  
Functor Seq Source #  
MonadFix Seq Source #  Since: 0.5.11 
Applicative Seq Source #  Since: 0.5.4 
Foldable Seq Source #  
Traversable Seq Source #  
Eq1 Seq Source #  Since: 0.5.9 
Ord1 Seq Source #  Since: 0.5.9 
Read1 Seq Source #  Since: 0.5.9 
Show1 Seq Source #  Since: 0.5.9 
MonadZip Seq Source # 

Alternative Seq Source #  Since: 0.5.4 
MonadPlus Seq Source #  
IsList (Seq a) Source #  
Eq a => Eq (Seq a) Source #  
Data a => Data (Seq a) Source #  
Ord a => Ord (Seq a) Source #  
Read a => Read (Seq a) Source #  
Show a => Show (Seq a) Source #  
(~) * a Char => IsString (Seq a) Source #  Since: 0.5.7 
Semigroup (Seq a) Source #  Since: 0.5.7 
Monoid (Seq a) Source #  
NFData a => NFData (Seq a) Source #  
type Item (Seq a) Source #  
Construction
(<) :: a > Seq a > Seq a infixr 5 Source #
\( O(1) \). Add an element to the left end of a sequence. Mnemonic: a triangle with the single element at the pointy end.
(>) :: Seq a > a > Seq a infixl 5 Source #
\( O(1) \). Add an element to the right end of a sequence. Mnemonic: a triangle with the single element at the pointy end.
(><) :: Seq a > Seq a > Seq a infixr 5 Source #
\( O(\log(\min(n_1,n_2))) \). Concatenate two sequences.
fromFunction :: Int > (Int > a) > Seq a Source #
\( O(n) \). Convert a given sequence length and a function representing that sequence into a sequence.
Since: 0.5.6.2
fromArray :: Ix i => Array i a > Seq a Source #
\( O(n) \). Create a sequence consisting of the elements of an Array
.
Note that the resulting sequence elements may be evaluated lazily (as on GHC),
so you must force the entire structure to be sure that the original array
can be garbagecollected.
Since: 0.5.6.2
Repetition
replicate :: Int > a > Seq a Source #
\( O(\log n) \). replicate n x
is a sequence consisting of n
copies of x
.
replicateA :: Applicative f => Int > f a > f (Seq a) Source #
replicateA
is an Applicative
version of replicate
, and makes
\( O(\log n) \) calls to liftA2
and pure
.
replicateA n x = sequenceA (replicate n x)
replicateM :: Applicative m => Int > m a > m (Seq a) Source #
replicateM
is a sequence counterpart of replicateM
.
replicateM n x = sequence (replicate n x)
For base >= 4.8.0
and containers >= 0.5.11
, replicateM
is a synonym for replicateA
.
cycleTaking :: Int > Seq a > Seq a Source #
O(log k).
forms a sequence of length cycleTaking
k xsk
by
repeatedly concatenating xs
with itself. xs
may only be empty if
k
is 0.
cycleTaking k = fromList . take k . cycle . toList
Iterative construction
iterateN :: Int > (a > a) > a > Seq a Source #
\( O(n) \). Constructs a sequence by repeated application of a function to a seed value.
iterateN n f x = fromList (Prelude.take n (Prelude.iterate f x))
unfoldr :: (b > Maybe (a, b)) > b > Seq a Source #
Builds a sequence from a seed value. Takes time linear in the number of generated elements. WARNING: If the number of generated elements is infinite, this method will not terminate.
Deconstruction
Queries
Views
View of the left end of a sequence.
Functor ViewL Source #  
Foldable ViewL Source #  
Traversable ViewL Source #  
Generic1 ViewL Source #  
Eq a => Eq (ViewL a) Source #  
Data a => Data (ViewL a) Source #  
Ord a => Ord (ViewL a) Source #  
Read a => Read (ViewL a) Source #  
Show a => Show (ViewL a) Source #  
Generic (ViewL a) Source #  
type Rep1 ViewL Source #  
type Rep (ViewL a) Source #  
View of the right end of a sequence.
EmptyR  empty sequence 
(Seq a) :> a infixl 5  the sequence minus the rightmost element, and the rightmost element 
Functor ViewR Source #  
Foldable ViewR Source #  
Traversable ViewR Source #  
Generic1 ViewR Source #  
Eq a => Eq (ViewR a) Source #  
Data a => Data (ViewR a) Source #  
Ord a => Ord (ViewR a) Source #  
Read a => Read (ViewR a) Source #  
Show a => Show (ViewR a) Source #  
Generic (ViewR a) Source #  
type Rep1 ViewR Source #  
type Rep (ViewR a) Source #  
Scans
Sublists
tails :: Seq a > Seq (Seq a) Source #
\( O(n) \). Returns a sequence of all suffixes of this sequence, longest first. For example,
tails (fromList "abc") = fromList [fromList "abc", fromList "bc", fromList "c", fromList ""]
Evaluating the \( i \)th suffix takes \( O(\log(\min(i, ni))) \), but evaluating every suffix in the sequence takes \( O(n) \) due to sharing.
inits :: Seq a > Seq (Seq a) Source #
\( O(n) \). Returns a sequence of all prefixes of this sequence, shortest first. For example,
inits (fromList "abc") = fromList [fromList "", fromList "a", fromList "ab", fromList "abc"]
Evaluating the \( i \)th prefix takes \( O(\log(\min(i, ni))) \), but evaluating every prefix in the sequence takes \( O(n) \) due to sharing.
chunksOf :: Int > Seq a > Seq (Seq a) Source #
\(O \Bigl(\bigl(\frac{n}{c}\bigr) \log c\Bigr)\). chunksOf c xs
splits xs
into chunks of size c>0
.
If c
does not divide the length of xs
evenly, then the last element
of the result will be short.
Side note: the given performance bound is missing some messy terms that only really affect edge cases. Performance degrades smoothly from \( O(1) \) (for \( c = n \)) to \( O(n) \) (for \( c = 1 \)). The true bound is more like \( O \Bigl( \bigl(\frac{n}{c}  1\bigr) (\log (c + 1)) + 1 \Bigr) \)
Since: 0.5.8
Sequential searches
takeWhileL :: (a > Bool) > Seq a > Seq a Source #
\( O(i) \) where \( i \) is the prefix length. takeWhileL
, applied
to a predicate p
and a sequence xs
, returns the longest prefix
(possibly empty) of xs
of elements that satisfy p
.
takeWhileR :: (a > Bool) > Seq a > Seq a Source #
\( O(i) \) where \( i \) is the suffix length. takeWhileR
, applied
to a predicate p
and a sequence xs
, returns the longest suffix
(possibly empty) of xs
of elements that satisfy p
.
is equivalent to takeWhileR
p xs
.reverse
(takeWhileL
p (reverse
xs))
dropWhileL :: (a > Bool) > Seq a > Seq a Source #
\( O(i) \) where \( i \) is the prefix length.
returns
the suffix remaining after dropWhileL
p xs
.takeWhileL
p xs
dropWhileR :: (a > Bool) > Seq a > Seq a Source #
\( O(i) \) where \( i \) is the suffix length.
returns
the prefix remaining after dropWhileR
p xs
.takeWhileR
p xs
is equivalent to dropWhileR
p xs
.reverse
(dropWhileL
p (reverse
xs))
spanl :: (a > Bool) > Seq a > (Seq a, Seq a) Source #
\( O(i) \) where \( i \) is the prefix length. spanl
, applied to
a predicate p
and a sequence xs
, returns a pair whose first
element is the longest prefix (possibly empty) of xs
of elements that
satisfy p
and the second element is the remainder of the sequence.
spanr :: (a > Bool) > Seq a > (Seq a, Seq a) Source #
\( O(i) \) where \( i \) is the suffix length. spanr
, applied to a
predicate p
and a sequence xs
, returns a pair whose first element
is the longest suffix (possibly empty) of xs
of elements that
satisfy p
and the second element is the remainder of the sequence.
breakl :: (a > Bool) > Seq a > (Seq a, Seq a) Source #
\( O(i) \) where \( i \) is the breakpoint index. breakl
, applied to a
predicate p
and a sequence xs
, returns a pair whose first element
is the longest prefix (possibly empty) of xs
of elements that
do not satisfy p
and the second element is the remainder of
the sequence.
partition :: (a > Bool) > Seq a > (Seq a, Seq a) Source #
\( O(n) \). The partition
function takes a predicate p
and a
sequence xs
and returns sequences of those elements which do and
do not satisfy the predicate.
filter :: (a > Bool) > Seq a > Seq a Source #
\( O(n) \). The filter
function takes a predicate p
and a sequence
xs
and returns a sequence of those elements which satisfy the
predicate.
Indexing
lookup :: Int > Seq a > Maybe a Source #
\( O(\log(\min(i,ni))) \). The element at the specified position,
counting from 0. If the specified position is negative or at
least the length of the sequence, lookup
returns Nothing
.
0 <= i < length xs ==> lookup i xs == Just (toList xs !! i)
i < 0  i >= length xs ==> lookup i xs = Nothing
Unlike index
, this can be used to retrieve an element without
forcing it. For example, to insert the fifth element of a sequence
xs
into a Map
m
at key k
, you could use
case lookup 5 xs of
Nothing > m
Just x > insert
k x m
Since: 0.5.8
(!?) :: Seq a > Int > Maybe a Source #
\( O(\log(\min(i,ni))) \). A flipped, infix version of lookup
.
Since: 0.5.8
index :: Seq a > Int > a Source #
\( O(\log(\min(i,ni))) \). The element at the specified position,
counting from 0. The argument should thus be a nonnegative
integer less than the size of the sequence.
If the position is out of range, index
fails with an error.
xs `index` i = toList xs !! i
Caution: index
necessarily delays retrieving the requested
element until the result is forced. It can therefore lead to a space
leak if the result is stored, unforced, in another structure. To retrieve
an element immediately without forcing it, use lookup
or '(!?)'.
adjust :: (a > a) > Int > Seq a > Seq a Source #
\( O(\log(\min(i,ni))) \). Update the element at the specified position. If
the position is out of range, the original sequence is returned. adjust
can lead to poor performance and even memory leaks, because it does not
force the new value before installing it in the sequence. adjust'
should
usually be preferred.
Since: 0.5.8
adjust' :: forall a. (a > a) > Int > Seq a > Seq a Source #
\( O(\log(\min(i,ni))) \). Update the element at the specified position. If the position is out of range, the original sequence is returned. The new value is forced before it is installed in the sequence.
adjust' f i xs = case xs !? i of Nothing > xs Just x > let !x' = f x in update i x' xs
Since: 0.5.8
update :: Int > a > Seq a > Seq a Source #
\( O(\log(\min(i,ni))) \). Replace the element at the specified position. If the position is out of range, the original sequence is returned.
take :: Int > Seq a > Seq a Source #
\( O(\log(\min(i,ni))) \). The first i
elements of a sequence.
If i
is negative,
yields the empty sequence.
If the sequence contains fewer than take
i si
elements, the whole sequence
is returned.
drop :: Int > Seq a > Seq a Source #
\( O(\log(\min(i,ni))) \). Elements of a sequence after the first i
.
If i
is negative,
yields the whole sequence.
If the sequence contains fewer than drop
i si
elements, the empty sequence
is returned.
insertAt :: Int > a > Seq a > Seq a Source #
\( O(\log(\min(i,ni))) \).
inserts insertAt
i x xsx
into xs
at the index i
, shifting the rest of the sequence over.
insertAt 2 x (fromList [a,b,c,d]) = fromList [a,b,x,c,d] insertAt 4 x (fromList [a,b,c,d]) = insertAt 10 x (fromList [a,b,c,d]) = fromList [a,b,c,d,x]
insertAt i x xs = take i xs >< singleton x >< drop i xs
Since: 0.5.8
deleteAt :: Int > Seq a > Seq a Source #
\( O(\log(\min(i,ni))) \). Delete the element of a sequence at a given index. Return the original sequence if the index is out of range.
deleteAt 2 [a,b,c,d] = [a,b,d] deleteAt 4 [a,b,c,d] = deleteAt (1) [a,b,c,d] = [a,b,c,d]
Since: 0.5.8
Indexing with predicates
These functions perform sequential searches from the left or right ends of the sequence, returning indices of matching elements.
elemIndexL :: Eq a => a > Seq a > Maybe Int Source #
elemIndexL
finds the leftmost index of the specified element,
if it is present, and otherwise Nothing
.
elemIndicesL :: Eq a => a > Seq a > [Int] Source #
elemIndicesL
finds the indices of the specified element, from
left to right (i.e. in ascending order).
elemIndexR :: Eq a => a > Seq a > Maybe Int Source #
elemIndexR
finds the rightmost index of the specified element,
if it is present, and otherwise Nothing
.
elemIndicesR :: Eq a => a > Seq a > [Int] Source #
elemIndicesR
finds the indices of the specified element, from
right to left (i.e. in descending order).
findIndexL :: (a > Bool) > Seq a > Maybe Int Source #
finds the index of the leftmost element that
satisfies findIndexL
p xsp
, if any exist.
findIndicesL :: (a > Bool) > Seq a > [Int] Source #
finds all indices of elements that satisfy findIndicesL
pp
,
in ascending order.
findIndexR :: (a > Bool) > Seq a > Maybe Int Source #
finds the index of the rightmost element that
satisfies findIndexR
p xsp
, if any exist.
findIndicesR :: (a > Bool) > Seq a > [Int] Source #
finds all indices of elements that satisfy findIndicesR
pp
,
in descending order.
Folds
foldlWithIndex :: (b > Int > a > b) > b > Seq a > b Source #
foldlWithIndex
is a version of foldl
that also provides access
to the index of each element.
foldrWithIndex :: (Int > a > b > b) > b > Seq a > b Source #
foldrWithIndex
is a version of foldr
that also provides access
to the index of each element.
Transformations
mapWithIndex :: (Int > a > b) > Seq a > Seq b Source #
A generalization of fmap
, mapWithIndex
takes a mapping
function that also depends on the element's index, and applies it to every
element in the sequence.
traverseWithIndex :: Applicative f => (Int > a > f b) > Seq a > f (Seq b) Source #
traverseWithIndex
is a version of traverse
that also offers
access to the index of each element.
Since: 0.5.8
intersperse :: a > Seq a > Seq a Source #
\( O(n) \). Intersperse an element between the elements of a sequence.
intersperse a empty = empty intersperse a (singleton x) = singleton x intersperse a (fromList [x,y]) = fromList [x,a,y] intersperse a (fromList [x,y,z]) = fromList [x,a,y,a,z]
Since: 0.5.8
Zips and unzips
zip :: Seq a > Seq b > Seq (a, b) Source #
\( O(\min(n_1,n_2)) \). zip
takes two sequences and returns a sequence
of corresponding pairs. If one input is short, excess elements are
discarded from the right end of the longer sequence.
unzipWith :: (a > (b, c)) > Seq a > (Seq b, Seq c) Source #
\( O(n) \). Unzip a sequence using a function to divide elements.
unzipWith f xs ==unzip
(fmap
f xs)
Efficiency note:
unzipWith
produces its two results in lockstep. If you calculate
unzipWith f xs
and fully force either of the results, then the
entire structure of the other one will be built as well. This
behavior allows the garbage collector to collect each calculated
pair component as soon as it dies, without having to wait for its mate
to die. If you do not need this behavior, you may be better off simply
calculating the sequence of pairs and using fmap
to extract each
component sequence.
Since: 0.5.11