rio-0.1.22.0: A standard library for Haskell

Safe Haskell | Safe-Inferred
Language | Haskell2010

RIO.List.Partial

Contents

- Basic functions
- Reducing lists (folds)
  - Special folds
- Building lists
  - Scans
- Indexing lists

Description

`List` partial functions. Import as:

    import qualified RIO.List.Partial as L'

Synopsis

- head :: HasCallStack => [a] -> a
- last :: HasCallStack => [a] -> a
- tail :: HasCallStack => [a] -> [a]
- init :: HasCallStack => [a] -> [a]
- foldl1 :: Foldable t => (a -> a -> a) -> t a -> a
- foldl1' :: HasCallStack => (a -> a -> a) -> [a] -> a
- foldr1 :: Foldable t => (a -> a -> a) -> t a -> a
- maximum :: (Foldable t, Ord a) => t a -> a
- minimum :: (Foldable t, Ord a) => t a -> a
- maximumBy :: Foldable t => (a -> a -> Ordering) -> t a -> a
- minimumBy :: Foldable t => (a -> a -> Ordering) -> t a -> a
- scanl1 :: (a -> a -> a) -> [a] -> [a]
- scanr1 :: (a -> a -> a) -> [a] -> [a]
- (!!) :: HasCallStack => [a] -> Int -> a

# Basic functions

head :: HasCallStack => [a] -> a Source #

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

    >>> head [1, 2, 3]
    1
    >>> head [1..]
    1
    >>> head []
    *** Exception: Prelude.head: empty list

WARNING: This function is partial. You can use case-matching, `uncons` or `listToMaybe` instead.

last :: HasCallStack => [a] -> a Source #

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

    >>> last [1, 2, 3]
    3
    >>> last [1..]
    * Hangs forever *
    >>> last []
    *** Exception: Prelude.last: empty list

WARNING: This function is partial. You can use `reverse` with case-matching, `uncons` or `listToMaybe` instead.

tail :: HasCallStack => [a] -> [a] Source #

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

    >>> tail [1, 2, 3]
    [2,3]
    >>> tail [1]
    []
    >>> tail []
    *** Exception: Prelude.tail: empty list

WARNING: This function is partial. You can use case-matching or `uncons` instead.

init :: HasCallStack => [a] -> [a] Source #

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

    >>> init [1, 2, 3]
    [1,2]
    >>> init [1]
    []
    >>> init []
    *** Exception: Prelude.init: empty list

WARNING: This function is partial. You can use `reverse` with case-matching or `uncons` instead.

# Reducing lists (folds)

foldl1 :: Foldable t => (a -> a -> a) -> t a -> a Source #

A variant of `foldl` that has no base case, and thus may only be applied to non-empty structures.

This function is non-total and will raise a runtime exception if the structure happens to be empty.

    foldl1 f = foldl1 f . toList

#### Examples

Expand

Basic usage:

    >>> foldl1 (+) [1..4]
    10

    >>> foldl1 (+) []
    *** Exception: Prelude.foldl1: empty list

    >>> foldl1 (+) Nothing
    *** Exception: foldl1: empty structure

    >>> foldl1 (-) [1..4]
    -8

    >>> foldl1 (&&) [True, False, True, True]
    False

    >>> foldl1 (||) [False, False, True, True]
    True

    >>> foldl1 (+) [1..]
    * Hangs forever *

foldl1' :: HasCallStack => (a -> a -> a) -> [a] -> a Source #

A strict version of `foldl1`.

foldr1 :: Foldable t => (a -> a -> a) -> t a -> a Source #

A variant of `foldr` that has no base case, and thus may only be applied to non-empty structures.

This function is non-total and will raise a runtime exception if the structure happens to be empty.

#### Examples

Expand

Basic usage:

    >>> foldr1 (+) [1..4]
    10

    >>> foldr1 (+) []
    Exception: Prelude.foldr1: empty list

    >>> foldr1 (+) Nothing
    *** Exception: foldr1: empty structure

    >>> foldr1 (-) [1..4]
    -2

    >>> foldr1 (&&) [True, False, True, True]
    False

    >>> foldr1 (||) [False, False, True, True]
    True

    >>> foldr1 (+) [1..]
    * Hangs forever *

## Special folds

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

The largest element of a non-empty structure.

This function is non-total and will raise a runtime exception if the structure happens to be empty. A structure that supports random access and maintains its elements in order should provide a specialised implementation to return the maximum in faster than linear time.

#### Examples

Expand

Basic usage:

    >>> maximum [1..10]
    10

    >>> maximum []
    *** Exception: Prelude.maximum: empty list

    >>> maximum Nothing
    *** Exception: maximum: empty structure

WARNING: This function is partial for possibly-empty structures like lists.

Since: base-4.8.0.0

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

The least element of a non-empty structure.

This function is non-total and will raise a runtime exception if the structure happens to be empty. A structure that supports random access and maintains its elements in order should provide a specialised implementation to return the minimum in faster than linear time.

#### Examples

Expand

Basic usage:

    >>> minimum [1..10]
    1

    >>> minimum []
    *** Exception: Prelude.minimum: empty list

    >>> minimum Nothing
    *** Exception: minimum: empty structure

WARNING: This function is partial for possibly-empty structures like lists.

Since: base-4.8.0.0

maximumBy :: Foldable t => (a -> a -> Ordering) -> t a -> a Source #

The largest element of a non-empty structure with respect to the given comparison function.

#### Examples

Expand

Basic usage:

    >>> maximumBy (compare `on` length) ["Hello", "World", "!", "Longest", "bar"]
    "Longest"

WARNING: This function is partial for possibly-empty structures like lists.

minimumBy :: Foldable t => (a -> a -> Ordering) -> t a -> a Source #

The least element of a non-empty structure with respect to the given comparison function.

#### Examples

Expand

Basic usage:

    >>> minimumBy (compare `on` length) ["Hello", "World", "!", "Longest", "bar"]
    "!"

WARNING: This function is partial for possibly-empty structures like lists.

# Building lists

## Scans

scanl1 :: (a -> a -> a) -> [a] -> [a] Source #

\(\mathcal{O}(n)\). `scanl1` is a variant of `scanl` that has no starting value argument:

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

    >>> scanl1 (+) [1..4]
    [1,3,6,10]
    >>> scanl1 (+) []
    []
    >>> scanl1 (-) [1..4]
    [1,-1,-4,-8]
    >>> scanl1 (&&) [True, False, True, True]
    [True,False,False,False]
    >>> scanl1 (||) [False, False, True, True]
    [False,False,True,True]
    >>> scanl1 (+) [1..]
    * Hangs forever *

scanr1 :: (a -> a -> a) -> [a] -> [a] Source #

\(\mathcal{O}(n)\). `scanr1` is a variant of `scanr` that has no starting value argument.

    >>> scanr1 (+) [1..4]
    [10,9,7,4]
    >>> scanr1 (+) []
    []
    >>> scanr1 (-) [1..4]
    [-2,3,-1,4]
    >>> scanr1 (&&) [True, False, True, True]
    [False,False,True,True]
    >>> scanr1 (||) [True, True, False, False]
    [True,True,False,False]
    >>> force $ scanr1 (+) [1..]
    *** Exception: stack overflow

# Indexing lists

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

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

    >>> ['a', 'b', 'c'] !! 0
    'a'
    >>> ['a', 'b', 'c'] !! 2
    'c'
    >>> ['a', 'b', 'c'] !! 3
    *** Exception: Prelude.!!: index too large
    >>> ['a', 'b', 'c'] !! (-1)
    *** Exception: Prelude.!!: negative index

WARNING: This function is partial. You can use <`atMay` https://hackage.haskell.org/package/safe-0.3.19/docs/Safe.html#v:atMay> instead.