HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to HaskellVariables (let)Data TypesOperatorsStringsListsConditions (if / then / else)Iteration (map / sum)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Value & Type
🔢 Numbers & Math
✍️ String
📚 List
🔀 Condition
🧩 Function
Haskell›Lists

Lists

List creation, accessing via head/!!, length, and concatenation with ++.

Simple enough for a 5-year-old to understand

Create and access a list

Lists are written with square brackets. head gives the first element, !! gives the element by index (from 0).

Example code
1
main = do
2
let xs = [10, 20, 30]
A list with three elements.
VariableA list with three elements.
3
print (head xs)
10 (first element)
Output / Input10 (first element)
4
print (xs !! 1)
20 (index 1)
Output / Input20 (index 1)
5
print (length xs)
3 (number of elements)
Output / Input3 (number of elements)
VariableOutput / Input
💡 Hint: head gives the first element; xs !! i gives the i-th element (0-indexed).

About this topic

Lists are Haskell's most used data structure. They are homogeneous and ordered; stored as a linked list, so prepending is cheap and appending is expensive.

Lists are evaluated lazily. This means you can define infinite lists: a list of all natural numbers is a valid value, and only the part you need is computed. This feature enables Haskell-specific solutions.

List comprehension is a special syntax resembling mathematical set notation. You write the source list, conditions, and transformation in a single expression; it replaces loops and filtering.

What you will learn

  • Creating lists and accessing elements
  • The prepend operator
  • List comprehension
  • The consequences of laziness
  • Safe access methods

Who is this for?

  • Learners of Haskell data structures
  • Those trying to access by index out of array habit
  • Those curious about infinite lists

When to use?

Almost all sequential data processing is done with lists; mapping, filtering, and folding operate on them.

If random access is required, the list is the wrong choice; array or map structures should be used.

Common mistakes

Wrong

main = print ([1, 2, 3] !! 10)

The index operator stops the program with "Prelude.!!: index too large" when going out of bounds. Also, this operator counts from the beginning, meaning random access is not as cheap as thought. Index access in lists is generally the wrong approach.

Correct

main = case drop 10 [1, 2, 3] of
  (x:_) -> print x
  []    -> putStrLn "out of bounds"

Wrong

main = print (1 ++ [2, 3])

The concatenation operator expects two lists; a single element is not a list and results in a type error. To prepend a single element, the cons operator is used. Mixing the two is a classic beginner mistake.

Correct

main = print (1 : [2, 3])

Wrong

main = print (length [1..])

The length of an infinite list cannot be computed; the program stops responding and eventually exhausts memory. Laziness allows you to define an infinite list, but it won't stop if you apply an operation that consumes it. A limiting operation must be used with infinite lists.

Correct

main = print (length (take 10 [1..]))

Frequently asked questions

How does list comprehension work?

It combines the source list, optional conditions, and the expression to be applied to each element into a single structure. If you provide multiple sources, all combinations are generated. It is a readable alternative to loops and filtering.

What are infinite lists good for?

When you don't know the number of elements to generate in advance. You define an infinite sequence, then take elements until a condition is met. Prime number generators and iterative calculations are written with this pattern.

Why is appending to the end of a list expensive?

Because the list is a linked structure; reaching the end requires traversing all elements from the start. Prepending the accumulation and reversing it at the end is a common and much faster solution.

Summary

Lists are homogeneous and linked in structure.

Prepending is cheap, appending is expensive.

Index access throws an error out of bounds.

Laziness makes it possible to define infinite lists.

List comprehension replaces loops and filtering.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code