| Safe Haskell | Trustworthy |
|---|---|
| Language | Haskell2010 |
Data.List.Compat
Documentation
module Data.List
compareLength :: [a] -> Int -> Ordering Source #
Use compareLength xs n as a safer and faster alternative
to compare (length xs) n. Similarly, it's better
to write compareLength xs 10 == LT instead of length xs < 10.
While length would force and traverse
the entire spine of xs (which could even diverge if xs is infinite),
compareLength traverses at most n elements to determine its result.
>>>compareLength [] 0EQ>>>compareLength [] 1LT>>>compareLength ['a'] 1EQ>>>compareLength ['a', 'b'] 1GT>>>compareLength [0..] 100GT>>>compareLength undefined (-1)GT>>>compareLength ('a' : undefined) 0GT
Since: 4.21.0.0
inits1 :: [a] -> [NonEmpty a] Source #
The inits1 function returns all non-empty initial segments of the
argument, shortest first.
Laziness
Note that inits1 has the following strictness property:
inits1 (xs ++ _|_) = inits1 xs ++ _|_
In particular,
inits1 _|_ = _|_
Examples
>>>inits1 "abc"['a' :| "",'a' :| "b",'a' :| "bc"]
>>>inits1 [][]
inits1 is productive on infinite lists:
>>>take 3 $ inits1 [1..][1 :| [],1 :| [2],1 :| [2,3]]
Since: 4.21.0.0
tails1 :: [a] -> [NonEmpty a] Source #
\(\mathcal{O}(n)\). The tails1 function returns all non-empty final
segments of the argument, longest first.
Laziness
Note that tails1 has the following strictness property:
tails1 _|_ = _|_
>>>tails1 undefined*** Exception: Prelude.undefined
>>>drop 1 (tails1 [undefined, 1, 2])[1 :| [2],2 :| []]
Examples
>>>tails1 "abc"['a' :| "bc",'b' :| "c",'c' :| ""]
>>>tails1 [1, 2, 3][1 :| [2,3],2 :| [3],3 :| []]
>>>tails1 [][]
Since: 4.21.0.0
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 Methods 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 Methods 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'] !? 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)