Lesson 05 of 55
9 mins readPython Strings, Lists & the Cons Operator (:)
In Plain English
In Haskell, lists are homogeneous (all elements must have the same type). Construct lists by prepending elements with the cons operator (:) or combining lists with (++).
Deep Dive: How It Works
List Syntax: [1, 2, 3] is syntactic sugar for 1 : 2 : 3 : [].
Cons Operator (:): Prepends an element to the front of a list in O(1) constant time.
Concatenation (++): Combines two lists in O(N) time proportional to the length of the first list.
String is [Char]: "Hello" is literally ['H', 'e', 'l', 'l', 'o'].
Core Rules to Remember

Cons (:) for O(1) Prepending: 1 : [2, 3] builds [1, 2, 3] instantly.

Concatenation (++): "Functional " ++ "Haskell" produces "Functional Haskell".
Live Interactive Example
Hit Run Code to see it liveConstructing Lists and Strings
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
Output Console
Click "Run Code" to view the rendered output.
How it works: 1 : 2 : [3, 4, 5] builds [1, 2, 3, 4, 5]. 'H' : "askell" builds "Haskell".
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Prepend Element with Cons
Prepend `0` to `[1, 2, 3]` with `0 : [1, 2, 3]`.
Print `"List: "` followed by `show` of the result.
1
2
3
4
5
6
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.