A list constructor builds a list. Haskell lists have two constructors: [] creates the empty list, while (:) prepends an element to an existing list. section 2.4.1
The two constructors describe every list:
- [] represents the empty list. section 2.4.1
- (x : xs) represents a nonempty list, with x as its head and xs as its tail. section 2.4.1
Constructing lists
The expression 1 : [2, 3] produces [1,2,3]. Chaining the constructor, 1 : 2 : 3 : [], produces the same list. section 2.4.1
Using constructors in patterns
Pattern matching can define behavior for both list shapes: listLength [] = 0 and listLength (x : xs) = 1 + listLength xs. These patterns are exhaustive for lists. section 2.4.1
ghci> [] [] ghci> 1 : [] [1] ghci> 1 : [2, 3] [1,2,3] ghci> 1 : 2 : 3 : [] [1,2,3]
Inspect interactive examples of [] and (:) building lists. section 2.4.1
