The empty list literal and the empty list of list literal

#1

I’m confused about these empty list(s) literals.

let emptyList = []
let emptyListList = [[]]
(*or even this one*)
let emptyListListList = [[[]]]

Why is it OK to use a [] where a [[]] is expected?

#2

When you match against [], you match the type 'a list.

The definition [[]] is just a specific case where 'a is 'b list, so the typechecker let you write [] instead of [[]].

1 Like
#3

emptyListList is not empty:

# List.hd emptyList;;
Exception: (Failure hd)
# List.hd emptyListList;;
- : 'a list = []
1 Like
#4

So should I use the literal [] for all empty list?

#5

There is only one empty list, []. The lists [[]] and [[[]]] are merely lists with one element.

4 Likes