HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

ModuleStream-0.4.7.2Haskell98

Data.Stream

Streams are infinite lists. Most operations on streams are completely analogous to the definition in Data.List.

The functions provided in this package are fairly careful about totality, termination, and productivity. None of the functions should diverge, provided you adhere to the preconditions mentioned in the documentation.

Note: I get quite a lot of requests regarding a missing Traversable instance for Streams. This has been left out by design.

Contents
  1. The type of streams
  2. Basic functions
  3. Stream transformations
  4. Building streams
  5. Extracting sublists
  6. Sublist predicates
  7. Indexing streams
  8. Zipping and unzipping streams
  9. Functions on streams of characters
  10. Converting to and from an infinite list
Synopsis
  • PackageStream-0.4.7.2
  • Exports48
  • LanguageHaskell98
  • LicenceBSD-3-Clause
  • SourceStream.hs

The type of streams

datadata Stream a

An infinite sequence.

Beware: If you use any function from the Eq or Ord class to compare two equal streams, these functions will diverge.

Constructors

Instances9Monad, Functor, Applicative, Eq, Ord, Show, …

Basic functions

valueinits :: Stream a -> Stream [a]

The inits function takes a stream xs and returns all the finite prefixes of xs.

Note that this inits is lazier then Data.List.inits:

inits _|_ = [] ::: _|_

while for Data.List.inits:

inits _|_ = _|_

Stream transformations

valuemap :: (a -> b) -> Stream a -> Stream b

Apply a function uniformly over all elements of a sequence.

valueinterleave :: Stream a -> Stream a -> Stream a

Interleave two Streams xs and ys, alternating elements from each list.

[x1,x2,...] `interleave` [y1,y2,...] == [x1,y1,x2,y2,...]
valuescan :: (a -> b -> a) -> a -> Stream b -> Stream a

scan yields a stream of successive reduced values from:

scan f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]
valuescan1 :: (a -> a -> a) -> Stream a -> Stream a

scan1 is a variant of scan that has no starting value argument:

scan1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]

Building streams

valueiterate :: (a -> a) -> a -> Stream a

iterate f x function produces the infinite sequence of repeated applications of f to x.

iterate f x = [x, f x, f (f x), ..]
valuecycle :: [a] -> Stream a

cycle xs returns the infinite repetition of xs:

cycle [1,2,3] = Cons 1 (Cons 2 (Cons 3 (Cons 1 (Cons 2 ...
valueunfold :: (c -> (a, c)) -> c -> Stream a

The unfold function is similar to the unfold for lists. Note there is no base case: all streams must be infinite.

valueprefix :: [a] -> Stream a -> Stream a

The prefix function adds a list as a prefix to an existing stream. If the list is infinite, it is converted to a Stream and the second argument is ignored.

Extracting sublists

valuetake :: Int -> Stream a -> [a]

take n xs returns the first n elements of xs.

Beware: passing a negative integer as the first argument will cause an error.

valuedrop :: Int -> Stream a -> Stream a

drop n xs drops the first n elements off the front of the sequence xs.

Beware: passing a negative integer as the first argument will cause an error.

valuesplitAt :: Int -> Stream a -> ([a], Stream a)

The splitAt function takes an integer n and a stream xs and returns a pair consisting of the prefix of xs of length n and the remaining stream immediately following this prefix.

Beware: passing a negative integer as the first argument will cause an error.

valuespan :: (a -> Bool) -> Stream a -> ([a], Stream a)

span p xs returns the longest prefix of xs that satisfies p, together with the remainder of the stream.

Beware: this function may diverge if every element of xs satisfies p, e.g. span even (repeat 0) will loop.

valuefilter :: (a -> Bool) -> Stream a -> Stream a

filter p xs, removes any elements from xs that do not satisfy p.

Beware: this function may diverge if there is no element of xs that satisfies p, e.g. filter odd (repeat 0) will loop.

valuepartition :: (a -> Bool) -> Stream a -> (Stream a, Stream a)

The partition function takes a predicate p and a stream xs, and returns a pair of streams. The first stream corresponds to the elements of xs for which p holds; the second stream corresponds to the elements of xs for which p does not hold.

Beware: One of the elements of the tuple may be undefined. For example, fst (partition even (repeat 0)) == repeat 0; on the other hand snd (partition even (repeat 0)) is undefined.

valuegroup :: Eq a => Stream a -> Stream [a]

The group function takes a stream and returns a stream of lists such that flattening the resulting stream is equal to the argument. Moreover, each sublist in the resulting stream contains only equal elements. For example,

group $ cycle "Mississippi" = "M" ::: "i" ::: "ss" ::: "i" ::: "ss" ::: "i" ::: "pp" ::: "i" ::: "M" ::: "i" ::: ...

Sublist predicates

valueisPrefixOf :: Eq a => [a] -> Stream a -> Bool

The isPrefix function returns True if the first argument is a prefix of the second.

Indexing streams

value(!!) :: Stream a -> Int -> a

xs !! n returns the element of the stream xs at index n. Note that the head of the stream has index 0.

Beware: passing a negative integer as the first argument will cause an error.

valueelemIndex :: Eq a => a -> Stream a -> Int

The elemIndex function returns the index of the first element in the given stream which is equal (by ==) to the query element,

Beware: elemIndex x xs will diverge if none of the elements of xs equal x.

valuefindIndex :: (a -> Bool) -> Stream a -> Int

The findIndex function takes a predicate and a stream and returns the index of the first element in the stream that satisfies the predicate,

Beware: findIndex p xs will diverge if none of the elements of xs satisfy p.

Zipping and unzipping streams

valuezip :: Stream a -> Stream b -> Stream (a, b)

The zip function takes two streams and returns the stream of pairs obtained by pairing elements at the same position in both argument streams.

Functions on streams of characters

The words function breaks a stream of characters into a stream of words, which were delimited by white space.

Beware: if the stream of characters xs does not contain white space, accessing the tail of words xs will loop.

The lines function breaks a stream of characters into a list of strings at newline characters. The resulting strings do not contain newlines.

Beware: if the stream of characters xs does not contain newline characters, accessing the tail of lines xs will loop.

Converting to and from an infinite list

valuefromList :: [a] -> Stream a

The fromList converts an infinite list to a stream.

Beware: Passing a finite list, will cause an error.