Lists
Lists are sequences of elements of the same type and are a key component of Haskell. This means that a list can only hold elements of the same type, e.g. a list of Ints
as we used in our example function - sum
. To create lists in Haskell, we put their elements in square brackets and separate them with commas:
Lists can also contain other lists:
But remember - lists are sequences of elements of the same type, so a list of lists must not contain lists of different types. For example, we cannot combine a list of Ints
and a list of Chars
into a single list of lists:
Lists can also be empty ([]
) and a special case called a singleton list is ([[]]
), which is a list with its single element being an empty list. Lists in Haskell can also be infinite.
Last updated