Safe Haskell | Safe-Inferred |
---|---|

Language | Haskell2010 |

## Synopsis

- head :: [a] -> a
- init :: [a] -> [a]
- tail :: [a] -> [a]
- last :: [a] -> a
- foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b
- foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
- foldl' :: Foldable t => (b -> a -> b) -> b -> t a -> b
- foldr' :: Foldable t => (a -> b -> b) -> b -> t a -> b
- foldr1 :: Foldable t => (a -> a -> a) -> t a -> a
- foldl1 :: Foldable t => (a -> a -> a) -> t a -> a
- cycle :: [a] -> [a]
- maximum :: (Foldable t, Ord a) => t a -> a
- minimum :: (Foldable t, Ord a) => t a -> a
- (!!) :: [a] -> Int -> a
- sum :: (Foldable t, Num a) => t a -> a
- product :: (Foldable t, Num a) => t a -> a
- fromJust :: HasCallStack => Maybe a -> a
- read :: Read a => String -> a

# Documentation

\(\mathcal{O}(n)\). Return all the elements of a list except the last one. The list must be non-empty.

\(\mathcal{O}(1)\). Extract the elements after the head of a list, which must be non-empty.

\(\mathcal{O}(n)\). Extract the last element of a list, which must be finite and non-empty.

foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b #

Left-associative fold of a structure.

In the case of lists, `foldl`

, when applied to a binary
operator, a starting value (typically the left-identity of the operator),
and a list, reduces the list using the binary operator, from left to
right:

foldl f z [x1, x2, ..., xn] == (...((z `f` x1) `f` x2) `f`...) `f` xn

Note that to produce the outermost application of the operator the
entire input list must be traversed. This means that `foldl'`

will
diverge if given an infinite list.

Also note that if you want an efficient left-fold, you probably want to
use `foldl'`

instead of `foldl`

. The reason for this is that latter does
not force the "inner" results (e.g. `z `f` x1`

in the above example)
before applying them to the operator (e.g. to `(`f` x2)`

). This results
in a thunk chain \(\mathcal{O}(n)\) elements long, which then must be
evaluated from the outside-in.

For a general `Foldable`

structure this should be semantically identical
to,

foldl f z =`foldl`

f z .`toList`

foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b #

Right-associative fold of a structure.

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,
`foldr`

can produce a terminating expression from an infinite list.

For a general `Foldable`

structure this should be semantically identical
to,

foldr f z =`foldr`

f z .`toList`

foldl' :: Foldable t => (b -> a -> b) -> b -> t a -> b #

Left-associative fold of a structure but with strict application of the operator.

This ensures that each step of the fold is forced to weak head normal
form before being applied, avoiding the collection of thunks that would
otherwise occur. This is often what you want to strictly reduce a finite
list to a single, monolithic result (e.g. `length`

).

For a general `Foldable`

structure this should be semantically identical
to,

foldl' f z =`foldl'`

f z .`toList`

*Since: base-4.6.0.0*

foldr' :: Foldable t => (a -> b -> b) -> b -> t a -> b #

Right-associative fold of a structure, but with strict application of the operator.

*Since: base-4.6.0.0*

`cycle`

ties a finite list into a circular one, or equivalently,
the infinite repetition of the original list. It is the identity
on infinite lists.

maximum :: (Foldable t, Ord a) => t a -> a #

The largest element of a non-empty structure.

*Since: base-4.8.0.0*

minimum :: (Foldable t, Ord a) => t a -> a #

The least element of a non-empty structure.

*Since: base-4.8.0.0*

(!!) :: [a] -> Int -> a infixl 9 #

List index (subscript) operator, starting from 0.
It is an instance of the more general `genericIndex`

,
which takes an index of any integral type.

sum :: (Foldable t, Num a) => t a -> a #

The `sum`

function computes the sum of the numbers of a structure.

*Since: base-4.8.0.0*

product :: (Foldable t, Num a) => t a -> a #

The `product`

function computes the product of the numbers of a
structure.

*Since: base-4.8.0.0*

fromJust :: HasCallStack => Maybe a -> a #

read :: Read a => String -> a #

The `read`

function reads input from a string, which must be
completely consumed by the input process. `read`

fails with an `error`

if the
parse is unsuccessful, and it is therefore discouraged from being used in
real applications. Use `readMaybe`

or `readEither`

for safe alternatives.

`>>>`

123`read "123" :: Int`

`>>>`

*** Exception: Prelude.read: no parse`read "hello" :: Int`