Lists

Contents
Build a list
Cons and concatenation
The basic operations
Partial functions

Build a list

A list holds values of one type, in order, and any number of them. The literal syntax with commas is sugar for a chain of cons operations:

xs = [1, 2, 3] ys = 1 : 2 : 3 : [] -- xs and ys are the same list

[] is the empty list and also the type constructor, so [Int] is [] Int underneath. The two spellings are interchangeable, and the bracketed one is what people write.

Cons and concatenation

: adds one element to the front in constant time. ++ copies its whole left argument, so the direction of a recursive build decides whether the function is fast or quadratic:

1 : [2,3] -- [1,2,3], cheap [1,2] ++ [3,4] -- [1,2,3,4], copies the left list -- good: build with : countdown 0 = [] countdown n = n : countdown (n - 1)

A list is a linked list, so prepending is free and appending is not. Every list function in the standard library respects that asymmetry.

The basic operations

These cover most traversal and inspection. Note that length walks the whole list, so checking for emptiness with it is a waste:

head [1,2,3] -- 1 tail [1,2,3] -- [2,3] length [1,2,3] -- 3 null [] -- True, constant time reverse [1,2,3] -- [3,2,1] take 2 [1,2,3] -- [1,2] drop 2 [1,2,3] -- [3]

take and drop accept counts larger than the list and return the obvious result instead of failing, which makes them safe in pipelines.

Partial functions

Four names in the list module throw an exception on an empty list: head, tail, last and init. They are convenient in GHCi and dangerous in a service:

-- crashes on [] head [] -- total, always returns a value safeHead :: [a] -> Maybe a safeHead (x:_) = Just x safeHead [] = Nothing

Pattern matching on the two constructors makes the empty case impossible to forget, which is the whole argument of lesson 22.

Next: Ranges and enumeration

Article author: Arthur Isaev

Related articles
Haskell
Haskell course: zero to hero in 50 lessons
What is Haskell and why it matters
Install the toolchain with GHCup
GHCi: the interactive shell
Your first program
Values with let and where
Types and type signatures
Functions and application
Currying and partial application
Operators, sections, and fixity
if, guards, and pattern guards
case expressions
Pattern matching
Tuples
Lists
Ranges and enumeration
List comprehensions
Recursion replaces loops
map, filter, and foldr
Lambdas and composition
Type classes
Parametric polymorphism
Maybe and null safety
Either and recoverable errors
data: product types
data: sum types
Records
newtype and type synonyms
Recursive data: binary trees
Functor
Applicative
Monad
do notation
IO and purity
Files and IO
Modules and imports
Cabal projects
Dependencies
Testing with HUnit
Property testing with QuickCheck
Laziness and infinite lists
Strictness and space leaks
Folds
Monoids
Traversable
Monad transformers
Errors and exceptions
Concurrency
Parsing with Megaparsec
Capstone: a todo CLI
Hero roadmap: the whole course on one page

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: