Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- data Infinite a = a :~ (Infinite a)
- data NonEmpty a = a :| [a]
- append :: [a] -> Infinite a -> Infinite a
- uncons :: Infinite a -> (a, Infinite a)
- concat :: Infinite [a] -> Infinite a
- intersperse :: a -> Infinite a -> Infinite a
- intercalate :: [a] -> Infinite [a] -> Infinite a
- subsequences :: Infinite a -> Infinite [a]
- foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
- scanl :: (b -> a -> b) -> b -> Infinite a -> Infinite b
- scanl' :: (b -> a -> b) -> b -> Infinite a -> Infinite b
- iterate :: (a -> a) -> a -> Infinite a
- iterate' :: (a -> a) -> a -> Infinite a
- repeat :: a -> Infinite a
- cycle :: NonEmpty a -> Infinite a
- unfoldr :: (b -> (a, b)) -> b -> Infinite a
- take :: Integral i => i -> Infinite a -> [a]
- drop :: Integral i => i -> Infinite a -> Infinite a
- splitAt :: Integral i => i -> Infinite a -> ([a], Infinite a)
- span :: (a -> Bool) -> Infinite a -> ([a], Infinite a)
- group :: Eq a => Infinite a -> Infinite [a]
- groupBy :: (a -> a -> Bool) -> Infinite a -> Infinite [a]
- inits :: Infinite a -> Infinite [a]
- isPrefixOf :: Eq a => [a] -> Infinite a -> Bool
- partition :: (a -> Bool) -> Infinite a -> (Infinite a, Infinite a)
- index :: Integral i => Infinite a -> i -> a
- zipWith :: (a -> b -> c) -> Infinite a -> Infinite b -> Infinite c
- unzip :: Infinite (a, b) -> (Infinite a, Infinite b)
- delete :: Eq a => a -> Infinite a -> Infinite a
- (\\) :: Eq a => Infinite a -> [a] -> Infinite a
- insert :: Ord a => a -> Infinite a -> Infinite a
- insertBy :: (a -> a -> Ordering) -> a -> Infinite a -> Infinite a
Definition
Instances
Foldable Infinite Source # | |
Defined in Data.List.Infinite fold :: Monoid m => Infinite m -> m # foldMap :: Monoid m => (a -> m) -> Infinite a -> m # foldMap' :: Monoid m => (a -> m) -> Infinite a -> m # foldr :: (a -> b -> b) -> b -> Infinite a -> b # foldr' :: (a -> b -> b) -> b -> Infinite a -> b # foldl :: (b -> a -> b) -> b -> Infinite a -> b # foldl' :: (b -> a -> b) -> b -> Infinite a -> b # foldr1 :: (a -> a -> a) -> Infinite a -> a # foldl1 :: (a -> a -> a) -> Infinite a -> a # elem :: Eq a => a -> Infinite a -> Bool # maximum :: Ord a => Infinite a -> a # minimum :: Ord a => Infinite a -> a # | |
Applicative Infinite Source # | |
Functor Infinite Source # | |
Non-empty (and non-strict) list type.
Since: base-4.9.0.0
a :| [a] infixr 5 |
Instances
Foldable NonEmpty | Since: base-4.9.0.0 |
Defined in Data.Foldable fold :: Monoid m => NonEmpty m -> m # foldMap :: Monoid m => (a -> m) -> NonEmpty a -> m # foldMap' :: Monoid m => (a -> m) -> NonEmpty a -> m # foldr :: (a -> b -> b) -> b -> NonEmpty a -> b # foldr' :: (a -> b -> b) -> b -> NonEmpty a -> b # foldl :: (b -> a -> b) -> b -> NonEmpty a -> b # foldl' :: (b -> a -> b) -> b -> NonEmpty a -> b # foldr1 :: (a -> a -> a) -> NonEmpty a -> a # foldl1 :: (a -> a -> a) -> NonEmpty a -> a # elem :: Eq a => a -> NonEmpty a -> Bool # maximum :: Ord a => NonEmpty a -> a # minimum :: Ord a => NonEmpty a -> a # | |
Traversable NonEmpty | Since: base-4.9.0.0 |
Applicative NonEmpty | Since: base-4.9.0.0 |
Functor NonEmpty | Since: base-4.9.0.0 |
Monad NonEmpty | Since: base-4.9.0.0 |
Semigroup (NonEmpty a) | Since: base-4.9.0.0 |
Read a => Read (NonEmpty a) | Since: base-4.11.0.0 |
Show a => Show (NonEmpty a) | Since: base-4.11.0.0 |
Eq a => Eq (NonEmpty a) | Since: base-4.9.0.0 |
Ord a => Ord (NonEmpty a) | Since: base-4.9.0.0 |
Basic functions
List transformations
intersperse :: a -> Infinite a -> Infinite a Source #
intercalate :: [a] -> Infinite [a] -> Infinite a Source #
subsequences :: Infinite a -> Infinite [a] Source #
Reducing lists (folds)
foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b #
Right-associative fold of a structure, lazy in the accumulator.
In the case of lists, foldr
, when applied to a binary operator, a
starting value (typically the right-identity of the operator), and a
list, reduces the list using the binary operator, from right to left:
foldr f z [x1, x2, ..., xn] == x1 `f` (x2 `f` ... (xn `f` z)...)
Note that since the head of the resulting expression is produced by an
application of the operator to the first element of the list, given an
operator lazy in its right argument, foldr
can produce a terminating
expression from an unbounded list.
For a general Foldable
structure this should be semantically identical
to,
foldr f z =foldr
f z .toList
Examples
Basic usage:
>>>
foldr (||) False [False, True, False]
True
>>>
foldr (||) False []
False
>>>
foldr (\c acc -> acc ++ [c]) "foo" ['a', 'b', 'c', 'd']
"foodcba"
Infinite structures
⚠️ Applying foldr
to infinite structures usually doesn't terminate.
It may still terminate under one of the following conditions:
- the folding function is short-circuiting
- the folding function is lazy on its second argument
Short-circuiting
(
short-circuits on ||
)True
values, so the following terminates
because there is a True
value finitely far from the left side:
>>>
foldr (||) False (True : repeat False)
True
But the following doesn't terminate:
>>>
foldr (||) False (repeat False ++ [True])
* Hangs forever *
Laziness in the second argument
Applying foldr
to infinite structures terminates when the operator is
lazy in its second argument (the initial accumulator is never used in
this case, and so could be left undefined
, but []
is more clear):
>>>
take 5 $ foldr (\i acc -> i : fmap (+3) acc) [] (repeat 1)
[1,4,7,10,13]