| Safe Haskell | Trustworthy |
|---|---|
| Language | Haskell2010 |
Data.List.Compat
Documentation
module Data.List
(!?) :: [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'] !? 0Just 'a'>>>['a', 'b', 'c'] !? 2Just 'c'>>>['a', 'b', 'c'] !? 3Nothing>>>['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)xsis theinitial part of the list andxis itslastelement.
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)