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?
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 [[]]
.
emptyListList
is not empty:
# List.hd emptyList;;
Exception: (Failure hd)
# List.hd emptyListList;;
- : 'a list = []
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.