Safe Haskell | None |
---|---|

Language | Haskell2010 |

- uncons :: [a] -> Maybe (a, [a])
- init :: [a] -> [a]
- tail :: [a] -> [a]
- reverse :: [a] -> [a]
- intersperse :: a -> [a] -> [a]
- intercalate :: [a] -> [[a]] -> [a]
- transpose :: [[a]] -> [[a]]
- subsequences :: [a] -> [[a]]
- permutations :: [a] -> [[a]]
- nub :: Ord a => [a] -> [a]
- fromList :: IsList l => [Item l] -> l
- scanl :: (b -> a -> b) -> b -> [a] -> [b]
- scanl' :: (b -> a -> b) -> b -> [a] -> [b]
- scanl1 :: (a -> a -> a) -> [a] -> [a]
- scanr :: (a -> b -> b) -> b -> [a] -> [b]
- scanr1 :: (a -> a -> a) -> [a] -> [a]
- iterate :: (a -> a) -> a -> [a]
- repeat :: a -> [a]
- replicate :: Int -> a -> [a]
- cycle :: [a] -> [a]
- unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
- unfoldl :: (b -> Maybe (b, a)) -> b -> [a]
- take :: Int -> [a] -> [a]
- drop :: Int -> [a] -> [a]
- splitAt :: Int -> [a] -> ([a], [a])
- takeWhile :: (a -> Bool) -> [a] -> [a]
- dropWhile :: (a -> Bool) -> [a] -> [a]
- group :: Eq a => [a] -> [[a]]
- inits :: [a] -> [[a]]
- tails :: [a] -> [[a]]
- filter :: (a -> Bool) -> [a] -> [a]
- partition :: (a -> Bool) -> [a] -> ([a], [a])
- zip :: [a] -> [b] -> [(a, b)]
- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
- unzip :: [(a, b)] -> ([a], [b])
- zip3 :: [a] -> [b] -> [c] -> [(a, b, c)]
- zipWith3 :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
- unzip3 :: [(a, b, c)] -> ([a], [b], [c])
- sort :: Ord a => [a] -> [a]
- sortOn :: Ord b => (a -> b) -> [a] -> [a]
- groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
- sortBy :: (a -> a -> Ordering) -> [a] -> [a]

# Basic functions

# List transformations

Return all the elements of a list except the last one. If the given list is empty, returns an empty list.

`>>>`

[1,2]`init [1,2,3]`

`>>>`

[]`init []`

Extract the elements after the head of a list. If the given list is empty, returns an empty list.

`>>>`

[2,3]`tail [1,2,3]`

`>>>`

[]`tail []`

intersperse :: a -> [a] -> [a] #

The `intersperse`

function takes an element and a list and
`intersperses' that element between the elements of the list.
For example,

intersperse ',' "abcde" == "a,b,c,d,e"

intercalate :: [a] -> [[a]] -> [a] #

`intercalate`

`xs xss`

is equivalent to `(`

.
It inserts the list `concat`

(`intersperse`

xs xss))`xs`

in between the lists in `xss`

and concatenates the
result.

The `transpose`

function transposes the rows and columns of its argument.
For example,

transpose [[1,2,3],[4,5,6]] == [[1,4],[2,5],[3,6]]

If some of the rows are shorter than the following rows, their elements are skipped:

transpose [[10,11],[20],[],[30,31,32]] == [[10,20,30],[11,31],[32]]

subsequences :: [a] -> [[a]] #

The `subsequences`

function returns the list of all subsequences of the argument.

subsequences "abc" == ["","a","b","ab","c","ac","bc","abc"]

permutations :: [a] -> [[a]] #

The `permutations`

function returns the list of all permutations of the argument.

permutations "abc" == ["abc","bac","cba","bca","cab","acb"]

# Building lists

fromList :: IsList l => [Item l] -> l #

The `fromList`

function constructs the structure `l`

from the given
list of `Item l`

## Scans

## Infinite lists

iterate :: (a -> a) -> a -> [a] #

`iterate`

`f x`

returns an infinite list of repeated applications
of `f`

to `x`

:

iterate f x == [x, f x, f (f x), ...]

replicate :: Int -> a -> [a] #

`replicate`

`n x`

is a list of length `n`

with `x`

the value of
every element.
It is an instance of the more general `genericReplicate`

,
in which `n`

may be of any integral type.

`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.

## Unfolding

unfoldr :: (b -> Maybe (a, b)) -> b -> [a] #

The `unfoldr`

function is a `dual' to `foldr`

: while `foldr`

reduces a list to a summary value, `unfoldr`

builds a list from
a seed value. The function takes the element and returns `Nothing`

if it is done producing the list or returns `Just`

`(a,b)`

, in which
case, `a`

is a prepended to the list and `b`

is used as the next
element in a recursive call. For example,

iterate f == unfoldr (\x -> Just (x, f x))

In some cases, `unfoldr`

can undo a `foldr`

operation:

unfoldr f' (foldr f z xs) == xs

if the following holds:

f' (f x y) = Just (x,y) f' z = Nothing

A simple use of unfoldr:

unfoldr (\b -> if b == 0 then Nothing else Just (b, b-1)) 10 [10,9,8,7,6,5,4,3,2,1]

# Sublists

## Extracting sublists

`take`

`n`

, applied to a list `xs`

, returns the prefix of `xs`

of length `n`

, or `xs`

itself if `n > `

:`length`

xs

take 5 "Hello World!" == "Hello" take 3 [1,2,3,4,5] == [1,2,3] take 3 [1,2] == [1,2] take 3 [] == [] take (-1) [1,2] == [] take 0 [1,2] == []

It is an instance of the more general `genericTake`

,
in which `n`

may be of any integral type.

`drop`

`n xs`

returns the suffix of `xs`

after the first `n`

elements, or `[]`

if `n > `

:`length`

xs

drop 6 "Hello World!" == "World!" drop 3 [1,2,3,4,5] == [4,5] drop 3 [1,2] == [] drop 3 [] == [] drop (-1) [1,2] == [1,2] drop 0 [1,2] == [1,2]

It is an instance of the more general `genericDrop`

,
in which `n`

may be of any integral type.

splitAt :: Int -> [a] -> ([a], [a]) #

`splitAt`

`n xs`

returns a tuple where first element is `xs`

prefix of
length `n`

and second element is the remainder of the list:

splitAt 6 "Hello World!" == ("Hello ","World!") splitAt 3 [1,2,3,4,5] == ([1,2,3],[4,5]) splitAt 1 [1,2,3] == ([1],[2,3]) splitAt 3 [1,2,3] == ([1,2,3],[]) splitAt 4 [1,2,3] == ([1,2,3],[]) splitAt 0 [1,2,3] == ([],[1,2,3]) splitAt (-1) [1,2,3] == ([],[1,2,3])

It is equivalent to `(`

when `take`

n xs, `drop`

n xs)`n`

is not `_|_`

(`splitAt _|_ xs = _|_`

).
`splitAt`

is an instance of the more general `genericSplitAt`

,
in which `n`

may be of any integral type.

takeWhile :: (a -> Bool) -> [a] -> [a] #

`takeWhile`

, applied to a predicate `p`

and a list `xs`

, returns the
longest prefix (possibly empty) of `xs`

of elements that satisfy `p`

:

takeWhile (< 3) [1,2,3,4,1,2,3,4] == [1,2] takeWhile (< 9) [1,2,3] == [1,2,3] takeWhile (< 0) [1,2,3] == []

group :: Eq a => [a] -> [[a]] #

The `group`

function takes a list and returns a list of lists such
that the concatenation of the result is equal to the argument. Moreover,
each sublist in the result contains only equal elements. For example,

group "Mississippi" = ["M","i","ss","i","ss","i","pp","i"]

It is a special case of `groupBy`

, which allows the programmer to supply
their own equality test.

# Searching with a predicate

filter :: (a -> Bool) -> [a] -> [a] #

`filter`

, applied to a predicate and a list, returns the list of
those elements that satisfy the predicate; i.e.,

filter p xs = [ x | x <- xs, p x]

partition :: (a -> Bool) -> [a] -> ([a], [a]) #

The `partition`

function takes a predicate a list and returns
the pair of lists of elements which do and do not satisfy the
predicate, respectively; i.e.,

partition p xs == (filter p xs, filter (not . p) xs)

# Zipping and unzipping lists

unzip :: [(a, b)] -> ([a], [b]) #

`unzip`

transforms a list of pairs into a list of first components
and a list of second components.

# Ordered lists

sortOn :: Ord b => (a -> b) -> [a] -> [a] #

Sort a list by comparing the results of a key function applied to each
element. `sortOn f`

is equivalent to `sortBy (comparing f)`

, but has the
performance advantage of only evaluating `f`

once for each element in the
input list. This is called the decorate-sort-undecorate paradigm, or
Schwartzian transform.

*Since: 4.8.0.0*

# Generalized functions

The predicate is assumed to define an equivalence.

The function is assumed to define a total ordering.