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] -> Intor
length :: List a -> IntThey 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 []
Data.String.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 3Examples
>>> ['H', 'a', 's', 'k', 'e', 'l', 'l']
"Haskell"
>>> 1 : [4, 1, 5, 9]
[1,4,1,5,9]
>>> [] : [] : []
[[],[]]
Instances23Monad, Functor, MonadFix, MonadFail, Applicative, Foldable, …
Monad []Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseFunctor []Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseMonadFix []Defined in ghc-internal-9.1003.0 · GHC.Internal.Control.Monad.FixMonadFail []Defined in ghc-internal-9.1003.0 · GHC.Internal.Control.Monad.FailApplicative []Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseFoldable []Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.FoldableTraversable []Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.TraversableAlternative []Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseCombines lists by concatenation, starting from the empty list.
MonadPlus []Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseCombines lists by concatenation, starting from the empty list.
Generic1 []Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsIsList [a]Defined in ghc-internal-9.1003.0 · GHC.Internal.IsListEq a => Eq [a]Defined in ghc-prim-0.12.0 · GHC.ClassesData a => Data [a]Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataFor historical reasons, the constructor name used for
(:)is"(:)". In a derived instance, it would be":".Ord a => Ord [a]Defined in ghc-prim-0.12.0 · GHC.ClassesRead a => Read [a]Defined in ghc-internal-9.1003.0 · GHC.Internal.ReadShow a => Show [a]Defined in ghc-internal-9.1003.0 · GHC.Internal.Showa ~ Char => IsString [a]Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.String(a ~ Char)context was introduced in4.9.0.0Generic [a]Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsSemigroup [a]Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseMonoid [a]Defined in ghc-internal-9.1003.0 · GHC.Internal.Basetype Rep [a] = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Generics"List"
"GHC.Types"
"ghc-prim"
'False) (C1 ('MetaCons"[]"
'PrefixI 'False) U1 :+: C1 ('MetaCons":"
('InfixI 'RightAssociative5
) 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a) :*: S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [a])))type Rep1 [] = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Generics"List"
"GHC.Types"
"ghc-prim"
'False) (C1 ('MetaCons"[]"
'PrefixI 'False) U1 :+: C1 ('MetaCons":"
('InfixI 'RightAssociative5
) 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1 :*: S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec1 [])))type Item [a] = aDefined in ghc-internal-9.1003.0 · GHC.Internal.IsList