I am answering question seven of Haskell's 99 questions. However, I have come to the point where they define the type
data NestedList a = Elem a | List [NestedList a]
and it, from my understanding, will not handle empty lists (ie. []).
But in their example tests they show
*Main> flatten (List [])
[]
Does this type cover empty lists? If so, why?
If it does not, and is a mistake of the websites, how would one write a nested list type that handles empty lists?
The datatype NestedList a contains either elements of type Elem a, or elements of type List [NestedList a].
The first of these, you already seem to understand. The second one, though, has as its argument a list (the normal sort) of NestedList a's. This can be any list, including []. Thus, List [] is a valid NestedList, as would be List[Elem 5], or List [Elem 5, List [Elem 3, Elem 2] ].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With