Safe Haskell | Trustworthy |
---|---|
Language | Haskell2010 |
Synopsis
- data List a
- module Data.Tuple.Experimental
- module Data.Sum.Experimental
Documentation
The builtin linked list type.
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 them unsuited for operations that require \(\mathcal{O}(1)\) access. Instead, they are intended to be traversed.
You can use List a
or [a]
in type signatures:
length :: [a] -> Int
or
length :: List a -> Int
They are fully equivalent, and List a
will be normalised to [a]
.
Usage
Lists are constructed recursively using the right-associative constructor operator (or cons)
(:) :: a -> [a] -> [a]
, which prepends an element to a list,
and the empty list []
.
(1 : 2 : 3 : []) == (1 : (2 : (3 : []))) == [1, 2, 3]
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 []
String
literals, like "I 💜 hs"
, are translated into
Lists of characters, ['I', ' ', '💜', ' ', 'h', 's']
.
Implementation
Internally and in memory, all the above are represented like this, with arrows being pointers to locations in memory.
╭───┬───┬──╮ ╭───┬───┬──╮ ╭───┬───┬──╮ ╭────╮ │(:)│ │ ─┼──>│(:)│ │ ─┼──>│(:)│ │ ─┼──>│ [] │ ╰───┴─┼─┴──╯ ╰───┴─┼─┴──╯ ╰───┴─┼─┴──╯ ╰────╯ v v v 1 2 3
Examples
>>> ['H', 'a', 's', 'k', 'e', 'l', 'l'] "Haskell"
>>> 1 : [4, 1, 5, 9] [1,4,1,5,9]
>>> [] : [] : [] [[],[]]
Since: ghc-internal-0.10.0
Instances
Eq1 [] Source # | Since: base-4.9.0.0 | ||||
Ord1 [] Source # | Since: base-4.9.0.0 | ||||
Defined in Data.Functor.Classes liftCompare :: (a -> b -> Ordering) -> [a] -> [b] -> Ordering Source # | |||||
Read1 [] Source # | Since: base-4.9.0.0 | ||||
Defined in Data.Functor.Classes | |||||
Show1 [] Source # | Since: base-4.9.0.0 | ||||
Alternative [] Source # | Combines lists by concatenation, starting from the empty list. Since: base-2.1 | ||||
Applicative [] Source # | Since: base-2.1 | ||||
Functor [] Source # | Since: base-2.1 | ||||
Monad [] Source # | Since: base-2.1 | ||||
MonadPlus [] Source # | Combines lists by concatenation, starting from the empty list. Since: base-2.1 | ||||
MonadFail [] Source # | Since: base-4.9.0.0 | ||||
Defined in GHC.Internal.Control.Monad.Fail | |||||
MonadFix [] Source # | Since: base-2.1 | ||||
Defined in GHC.Internal.Control.Monad.Fix | |||||
MonadZip [] Source # | Since: ghc-internal-4.3.1.0 | ||||
Foldable [] Source # | Since: base-2.1 | ||||
Defined in GHC.Internal.Data.Foldable fold :: Monoid m => [m] -> m Source # foldMap :: Monoid m => (a -> m) -> [a] -> m Source # foldMap' :: Monoid m => (a -> m) -> [a] -> m Source # foldr :: (a -> b -> b) -> b -> [a] -> b Source # foldr' :: (a -> b -> b) -> b -> [a] -> b Source # foldl :: (b -> a -> b) -> b -> [a] -> b Source # foldl' :: (b -> a -> b) -> b -> [a] -> b Source # foldr1 :: (a -> a -> a) -> [a] -> a Source # foldl1 :: (a -> a -> a) -> [a] -> a Source # elem :: Eq a => a -> [a] -> Bool Source # maximum :: Ord a => [a] -> a Source # minimum :: Ord a => [a] -> a Source # | |||||
Traversable [] Source # | Since: base-2.1 | ||||
Generic1 [] Source # | |||||
Defined in GHC.Internal.Generics
| |||||
Lift a => Lift ([a] :: Type) Source # | |||||
IsChar c => PrintfArg [c] Source # | Since: base-2.1 | ||||
Defined in Text.Printf formatArg :: [c] -> FieldFormatter Source # parseFormat :: [c] -> ModifierParser Source # | |||||
IsChar c => PrintfType [c] Source # | Since: base-2.1 | ||||
Defined in Text.Printf | |||||
Monoid [a] Source # | Since: base-2.1 | ||||
Semigroup [a] Source # | Since: base-4.9.0.0 | ||||
Eq a => Eq [a] Source # | |||||
Ord a => Ord [a] Source # | |||||
Data a => Data [a] Source # | For historical reasons, the constructor name used for Since: base-4.0.0.0 | ||||
Defined in GHC.Internal.Data.Data gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> [a] -> c [a] Source # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c [a] Source # toConstr :: [a] -> Constr Source # dataTypeOf :: [a] -> DataType Source # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c [a]) Source # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c [a]) Source # gmapT :: (forall b. Data b => b -> b) -> [a] -> [a] Source # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> [a] -> r Source # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> [a] -> r Source # gmapQ :: (forall d. Data d => d -> u) -> [a] -> [u] Source # gmapQi :: Int -> (forall d. Data d => d -> u) -> [a] -> u Source # gmapM :: Monad m => (forall d. Data d => d -> m d) -> [a] -> m [a] Source # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> [a] -> m [a] Source # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> [a] -> m [a] Source # | |||||
a ~ Char => IsString [a] Source # |
Since: base-2.1 | ||||
Defined in GHC.Internal.Data.String fromString :: String -> [a] Source # | |||||
Generic [a] Source # | |||||
Defined in GHC.Internal.Generics
| |||||
IsList [a] Source # | Since: base-4.7.0.0 | ||||
Read a => Read [a] Source # | Since: base-2.1 | ||||
Show a => Show [a] Source # | Since: base-2.1 | ||||
type Rep1 [] Source # | Since: base-4.6.0.0 | ||||
Defined in GHC.Internal.Generics type Rep1 [] = D1 ('MetaData "List" "GHC.Internal.Types" "ghc-internal" '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] Source # | Since: base-4.6.0.0 | ||||
Defined in GHC.Internal.Generics type Rep [a] = D1 ('MetaData "List" "GHC.Internal.Types" "ghc-internal" '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] Source # | |||||
Defined in GHC.Internal.IsList type Item [a] = a |
Tuple/sum syntax families and tycons
module Data.Tuple.Experimental
module Data.Sum.Experimental