WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Haskell · Basic · question 9 of 100

How are lists defined in Haskell? Provide an example.?

📕 Buy this interview preparation book: 100 Haskell questions & answers — PDF + EPUB for $5

In Haskell, lists are defined as a homogeneous data structure that stores elements of the same type. They can be constructed using the ‘:‘ operator (cons) and the empty list ‘[]‘. Lists are recursive, and each element is linked to the next using the cons operator.

Here’s an example list of integers:

intList :: [Int]
intList = 1 : 2 : 3 : 4 : 5 : []

Let’s examine the Haskell list more in-depth in LaTeX format.

A list of elements of type ‘a‘ can be represented as ‘[a]‘. The type signature of the cons operator ‘:‘ is:

(:) :: a -> [a] -> [a]

This means that the cons operator ‘:‘ takes an element of type ‘a‘ and a list of elements of type ‘[a]‘, and then returns a new list of elements of type ‘[a]‘.

Given the list of integers ‘intList‘ from above, we can represent the construction of the list in LaTeX:


intList = 1 : (2 : (3 : (4 : (5 : []))))

In Haskell, you can also define lists using the literal syntax, which is more concise:

intList :: [Int]
intList = [1, 2, 3, 4, 5]

Here is a chart visualizing how ‘intList‘ is represented as a linked list:

In this chart, indices are the positions of each element in the list, values are the elements of the list, the colons ‘:‘ represent the cons operator, and the empty brackets ‘[]‘ denote the end of the list.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Haskell interview — then scores it.
📞 Practice Haskell — free 15 min
📕 Buy this interview preparation book: 100 Haskell questions & answers — PDF + EPUB for $5

All 100 Haskell questions · All topics