[Haskell-beginners] A problem about Haskell 99 questions

Daniel Fischer daniel.is.fischer at googlemail.com
Tue May 17 12:02:55 CEST 2011


On Tuesday 17 May 2011 11:51:24, amazingjxq wrote:
> In problem 7 <http://www.haskell.org/haskellwiki/99_questions/1_to_10>,
> the example in haskell is below:
> 
> *Main> flatten (Elem 5)
> [5]
> *Main> flatten (List [Elem 1, List [Elem 2, List [Elem 3, Elem 4], Elem 
5]])
> [1,2,3,4,5]
> *Main> flatten (List [])
> []
> 
> >  What are the functions List and Elem? I cann't hoogle it.

They're the constructors of a datatype defined specifically for this 
problem. While in Lisp, you can put different types of things in the same 
list, Haskell lists are homogeneous (every element has the same type).
So to create nested lists, you have to create a type T that can hold lists 
of type T, like

data NestedList a
    = Elem a
    | List [NestedList a]

(and if you follow the 'Solutions' link at the problem, you find the source 
where this is defined).



More information about the Beginners mailing list