| Safe Haskell | Trustworthy |
|---|---|
| Language | Haskell2010 |
Data.List.Compat
Documentation
module Data.List
compareLength :: [a] -> Int -> Ordering #
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] #
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] #
\(\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