Safe Haskell | Trustworthy |
---|---|
Language | Haskell2010 |
Documentation
module Data.List
The builtin list type, usually written in its non-prefix form [a]
.
Examples
Unless the OverloadedLists extension is enabled, list literals are
syntactic sugar for repeated applications of :
and []
.
>>>
1:2:3:4:[] == [1,2,3,4]
True
Similarly, unless the OverloadedStrings extension is enabled, string literals are syntactic sugar for a lists of characters.
>>>
['h','e','l','l','o'] == "hello"
True
Instances
MonadFail List | Since: base-4.9.0.0 |
Defined in Control.Monad.Fail | |
Foldable List | Since: base-2.1 |
Defined in Data.Foldable fold :: Monoid m => [m] -> m # foldMap :: Monoid m => (a -> m) -> [a] -> m # foldMap' :: Monoid m => (a -> m) -> [a] -> m # foldr :: (a -> b -> b) -> b -> [a] -> b # foldr' :: (a -> b -> b) -> b -> [a] -> b # foldl :: (b -> a -> b) -> b -> [a] -> b # foldl' :: (b -> a -> b) -> b -> [a] -> b # foldr1 :: (a -> a -> a) -> [a] -> a # foldl1 :: (a -> a -> a) -> [a] -> a # elem :: Eq a => a -> [a] -> Bool # maximum :: Ord a => [a] -> a # | |
Traversable List | Since: base-2.1 |
Defined in Data.Traversable | |
Alternative List | Combines lists by concatenation, starting from the empty list. Since: base-2.1 |
Applicative List | Since: base-2.1 |
Functor List | Since: base-2.1 |
Monad List | Since: base-2.1 |
MonadPlus List | Combines lists by concatenation, starting from the empty list. Since: base-2.1 |
a ~ Char => IsString [a] |
Since: base-2.1 |
Defined in Data.String fromString :: String -> [a] # | |
Monoid [a] | Since: base-2.1 |
Semigroup [a] | Since: base-4.9.0.0 |
Read a => Read [a] | Since: base-2.1 |
Show a => Show [a] | Since: base-2.1 |
Eq a => Eq [a] | |
Ord a => Ord [a] | |
(!?) :: [a] -> Int -> Maybe a infixl 9 Source #
List index (subscript) operator, starting from 0. Returns Nothing
if the index is out of bounds
>>>
['a', 'b', 'c'] !? 0
Just 'a'>>>
['a', 'b', 'c'] !? 2
Just 'c'>>>
['a', 'b', 'c'] !? 3
Nothing>>>
['a', 'b', 'c'] !? (-1)
Nothing
This is the total variant of the partial !!
operator.
WARNING: This function takes linear time in the index.
unsnoc :: [a] -> Maybe ([a], a) Source #
\(\mathcal{O}(n)\). Decompose a list into init
and last
.
- If the list is empty, returns
Nothing
. - If the list is non-empty, returns
, whereJust
(xs, x)xs
is theinit
ial part of the list andx
is itslast
element.
Since: 4.19.0.0
>>>
unsnoc []
Nothing>>>
unsnoc [1]
Just ([],1)>>>
unsnoc [1, 2, 3]
Just ([1,2],3)
Laziness:
>>>
fst <$> unsnoc [undefined]
Just []>>>
head . fst <$> unsnoc (1 : undefined)
Just *** Exception: Prelude.undefined>>>
head . fst <$> unsnoc (1 : 2 : undefined)
Just 1
unsnoc
is dual to uncons
: for a finite list xs
unsnoc xs = (\(hd, tl) -> (reverse tl, hd)) <$> uncons (reverse xs)