Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does this type definition handle empty lists?

Tags:

list

haskell

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?

like image 528
sdasdadas Avatar asked Aug 24 '26 23:08

sdasdadas


1 Answers

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] ].

like image 173
qaphla Avatar answered Aug 27 '26 21:08

qaphla