[Haskell-cafe] lists of arbitrary depth

Vo Minh Thu noteed at gmail.com
Tue Jul 13 05:17:31 EDT 2010


2010/7/13 Ivan Lazar Miljenovic <ivan.miljenovic at gmail.com>:
> vadali <shlomivaknin at gmail.com> writes:
>
>> hello,
>> iam really new to haskell,
>>
>> i want to define a function which takes as a parameter a list which can
>> contain other lists, eg. [1,[2,3],[4,[5,6]]]
>>
>> how would i define a function that can iterate through the items so (in this
>> example)
>> iter1 = 1
>> iter2 = [2,3]
>> iter3 = [4,[5,6]]
>>
>> ?
>>
>> ( can i do that without using the Tree data type? )
>
> Well, that's what a tree is, so why not use a tree?
>
> Your only other option is to define your own tree-like structure:
>
> data MyTree a = Value a | SubTree [MyTree a]

Defining your own tree structure is better in this case as Data.Tree
store elements in each node, not just in the leaves.

Cheers,
Thu


More information about the Haskell-Cafe mailing list