The empty list literal and the empty list of list literal

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?

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

emptyListList is not empty:

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

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

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