| 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].
In Haskell, lists are one of the most important data types as they are often used analogous to loops in imperative programming languages. These lists are singly linked, which makes it unsuited for operations that require \(\mathcal{O}(1)\) access. Instead, lists are intended to be traversed.
Lists are constructed recursively using the right-associative cons-operator
(:) :: a -> [a] -> [a], which prepends an element to a list,
and the empty list [].
(1 : 2 : 3 : []) == (1 : (2 : (3 : []))) == [1, 2, 3]
Internally and in memory, all the above are represented like this, with arrows being pointers to locations in memory.
╭───┬───┬──╮ ╭───┬───┬──╮ ╭───┬───┬──╮ ╭────╮
│(:)│ │ ─┼──>│(:)│ │ ─┼──>│(:)│ │ ─┼──>│ [] │
╰───┴─┼─┴──╯ ╰───┴─┼─┴──╯ ╰───┴─┼─┴──╯ ╰────╯
v v v
1 2 3As seen above, lists can also be constructed using list literals
of the form [x_1, x_2, ..., x_n]
which are syntactic sugar and, unless -XOverloadedLists is enabled,
are translated into uses of (:) and []
Similarly, String literals of the form "I 💜 hs" are translated into
Lists of characters, ['I', ' ', '💜', ' ', 'h', 's'].
Examples
>>> ['H', 'a', 's', 'k', 'e', 'l', 'l'] "Haskell"
>>> 1 : [4, 1, 5, 9] [1,4,1,5,9]
>>> [] : [] : [] [[],[]]
Since: ghc-prim-0.10.0
Instances
| MonadFail [] | Since: base-4.9.0.0 | ||||
Defined in Control.Monad.Fail | |||||
| MonadFix [] | Since: base-2.1 | ||||
Defined in Control.Monad.Fix | |||||
| MonadZip [] | Since: base-4.3.1.0 | ||||
| Foldable [] | 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 # | |||||
| Eq1 [] | Since: base-4.9.0.0 | ||||
Defined in Data.Functor.Classes | |||||
| Ord1 [] | Since: base-4.9.0.0 | ||||
Defined in Data.Functor.Classes Methods liftCompare :: (a -> b -> Ordering) -> [a] -> [b] -> Ordering # | |||||
| Read1 [] | Since: base-4.9.0.0 | ||||
Defined in Data.Functor.Classes | |||||
| Show1 [] | Since: base-4.9.0.0 | ||||
Defined in Data.Functor.Classes | |||||
| Traversable [] | Since: base-2.1 | ||||
Defined in Data.Traversable | |||||
| Alternative [] | Combines lists by concatenation, starting from the empty list. Since: base-2.1 | ||||
| Applicative [] | Since: base-2.1 | ||||
| Functor [] | Since: base-2.1 | ||||
| Monad [] | Since: base-2.1 | ||||
| MonadPlus [] | Combines lists by concatenation, starting from the empty list. Since: base-2.1 | ||||
| Generic1 [] | |||||
Defined in GHC.Generics Associated Types
| |||||
| Data a => Data [a] | For historical reasons, the constructor name used for Since: base-4.0.0.0 | ||||
Defined in Data.Data Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> [a] -> c [a] # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c [a] # dataTypeOf :: [a] -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c [a]) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c [a]) # gmapT :: (forall b. Data b => b -> b) -> [a] -> [a] # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> [a] -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> [a] -> r # gmapQ :: (forall d. Data d => d -> u) -> [a] -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> [a] -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> [a] -> m [a] # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> [a] -> m [a] # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> [a] -> m [a] # | |||||
| 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 | ||||
| Generic [a] | |||||
Defined in GHC.Generics Associated Types
| |||||
| IsList [a] | Since: base-4.7.0.0 | ||||
| Read a => Read [a] | Since: base-2.1 | ||||
| Show a => Show [a] | Since: base-2.1 | ||||
| IsChar c => PrintfArg [c] | Since: base-2.1 | ||||
Defined in Text.Printf | |||||
| IsChar c => PrintfType [c] | Since: base-2.1 | ||||
Defined in Text.Printf | |||||
| Eq a => Eq [a] | |||||
| Ord a => Ord [a] | |||||
| type Rep1 [] | Since: base-4.6.0.0 | ||||
Defined in GHC.Generics type Rep1 [] = D1 ('MetaData "List" "GHC.Types" "ghc-prim" 'False) (C1 ('MetaCons "[]" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons ":" ('InfixI 'RightAssociative 5) 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1 :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec1 []))) | |||||
| type Rep [a] | Since: base-4.6.0.0 | ||||
Defined in GHC.Generics type Rep [a] = D1 ('MetaData "List" "GHC.Types" "ghc-prim" 'False) (C1 ('MetaCons "[]" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons ":" ('InfixI 'RightAssociative 5) 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [a]))) | |||||
| type Item [a] | |||||
Defined in GHC.IsList type Item [a] = a | |||||