Mikan
Safe HaskellNone
LanguageHaskell2010

Mikan.Utils.ListInf

Description

Infinite lists (streams).

Wrapper for Data.List.Infinite from the infinite-list package that generalizes some type signatures, using Integral for indexing, and adds some missing functions.

Import this module as follows:

import Mikan.Utils.ListInf (ListInf, pattern (:<))
import Mikan.Utils.ListInf qualified as ListInf

Motivation: lists constructed by repeat, iterate, cycle etc. are infinite, so the nil case can never arise when consuming them, e.g. in a take or zipWith. Constructing them as properly infinite lists (ListInf) should thus lead to slightly more efficient code, eliminating impossible cases.

Synopsis

Documentation

Construction

append :: [a] -> ListInf a -> ListInf a Source #

There is only one meaningful way to concatenate a finite and an infinite list, justifying the name append = prependList.

pad :: [a] -> a -> ListInf a Source #

pad as b implements the frequent pattern as ++ repeat b.

upFrom :: Enum n => n -> ListInf n Source #

upFrom n = [n..] as properly infinite list.

Selection

updateAt :: Integral n => n -> (a -> a) -> ListInf a -> ListInf a Source #

\(\Theta(n)\). Update the nth element of an infinite list.

Precondition: the index n is >= 0.

Generalized versions of functions from infinite-list

(!!) :: Integral n => ListInf a -> n -> a Source #

\(\Theta(n)\). Get the nth element of an infinite list.

Unlike !! for List, this function is total.

take :: Integral n => n -> ListInf a -> [a] Source #

\(\Theta(n)\). Take n elements from an infinite list.

drop :: Integral n => n -> ListInf a -> ListInf a Source #

\(\Theta(n)\). Drop n elements from an infinite list.

splitAt :: Integral n => n -> ListInf a -> ([a], ListInf a) Source #

\(\Theta(n)\). Split an infinite list at the nth element.

Laws

Expand
splitAt n xs = (take n xs, drop n xs)

Re-exports

cycle :: NonEmpty a -> Infinite a #

Repeat a non-empty list ad infinitum. If you were looking for something like fromList :: [a] -> Infinite a, look no further.

It would be less annoying to take [a] instead of NonEmpty a, but we strive to avoid partial functions.

head :: Infinite a -> a #

Get the first elements of an infinite list.

iterate :: (a -> a) -> a -> Infinite a #

Generate an infinite list of repeated applications.

prependList :: [a] -> Infinite a -> Infinite a #

Prepend a list to an infinite list.

repeat :: a -> Infinite a #

Repeat the same element ad infinitum.

tail :: Infinite a -> Infinite a #

Get the elements of an infinite list after the first one.

zip :: Infinite a -> Infinite b -> Infinite (a, b) #

Zip two infinite lists.

zipWith :: (a -> b -> c) -> Infinite a -> Infinite b -> Infinite c #

Zip two infinite lists with a given function.

pattern (:<) :: a -> Infinite a -> Infinite a infixr 5 #