
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? ) THANKS! -- View this message in context: http://old.nabble.com/lists-of-arbitrary-depth-tp29139641p29139641.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

On 13 July 2010 10:58, vadali
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? )
Maybe this should be moved to Haskell-Beginnners. It's impossible to have a function that works on a list of arbitrary nesting. But you could use a tree data type, yeah.

On Jul 13, 2010, at 9:00 PM, Christopher Done wrote:
On 13 July 2010 10:58, vadali
wrote:\ 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]]]
What would the type of a list like that be? What you _can_ do is data List_Or t = Item t | List [List_Or t] deriving (Eq, Ord, Show) and have a "list" like ell :: List_Or Int ell = List [Item 1, List [Item 2, Item 3], List [Item 4, List [Item 5, Item 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]]
Then you can write functions like iter n (List x) = head (drop (n-1) x) with examples *Main> iter 1 ell Item 1 *Main> iter 2 ell List [Item 2,Item 3] *Main> iter 3 ell List [Item 4,List [Item 5,Item 6]]
( can i do that without using the Tree data type? )
This basically _is_ a tree data type, even if not THE Tree data type. That's because nested lists are trees.

Hi Vadali,
On Tue, Jul 13, 2010 at 10:58 AM, vadali
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?
No, a list contains a homogeneous sequence of values (i.e. a sequence of values of the same type). If you explain what you're trying to achieve we might be able to offer you better help. Cheers, Johan

vadali
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]
THANKS!
YOUR WELCOME! :p -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

Thank you all for replying! I am really beginning my baby steps in this fascinating language, and was just wondering if it was possible to naturally scan lists with arbitrary lists (aka trees :) ). I guess this forum specifically is too advancaed for me (yet!), so my next questions will be posted on the beginners forum. Thanks, Vadali On Tue, Jul 13, 2010 at 11:07 AM, Ivan Lazar Miljenovic < ivan.miljenovic@gmail.com> wrote:
vadali
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]
THANKS!
YOUR WELCOME! :p
-- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

On 13 Jul 2010, at 10:11, Shlomi Vaknin wrote:
Thank you all for replying!
I am really beginning my baby steps in this fascinating language, and was just wondering if it was possible to naturally scan lists with arbitrary lists (aka trees :) ).
Trees aren't lists, Trees are trees... Here's how you create one* data Tree a = Leaf | Branch a [Tree a] This roughly says, a tree can be made up of two possible things... First, it could be a leaf, and secondly, it could be a branch with an element at it, and a list of subtrees. Some example trees using this data type: 1) Leaf 2) Branch 5 [] 3) Branch 5 [Leaf] 4) Branch 5 [Branch 10 [], Branch 20 [], Leaf, Branch 50 [Branch 10 [], Leaf]] When you have really strong typing, you also have very well specified types. You don't just use a list as if it were a tree, you declare what a tree is. Bob * In this case an arbitrarily branching tree, we could ofc declare different forms of tree here.

Thank you Bob,
your example clarified how actually using such data type would appear in
haskell. I naively thought it would be as simple as defining a regular list,
but i see it is slightly more strict than that. I appreciate your help!
Vadali
On Tue, Jul 13, 2010 at 11:18 AM, Thomas Davie
On 13 Jul 2010, at 10:11, Shlomi Vaknin wrote:
Thank you all for replying!
I am really beginning my baby steps in this fascinating language, and was just wondering if it was possible to naturally scan lists with arbitrary lists (aka trees :) ).
Trees aren't lists, Trees are trees... Here's how you create one*
data Tree a = Leaf | Branch a [Tree a]
This roughly says, a tree can be made up of two possible things... First, it could be a leaf, and secondly, it could be a branch with an element at it, and a list of subtrees.
Some example trees using this data type:
1) Leaf 2) Branch 5 [] 3) Branch 5 [Leaf] 4) Branch 5 [Branch 10 [], Branch 20 [], Leaf, Branch 50 [Branch 10 [], Leaf]]
When you have really strong typing, you also have very well specified types. You don't just use a list as if it were a tree, you declare what a tree is.
Bob
* In this case an arbitrarily branching tree, we could ofc declare different forms of tree here.

On Tue, Jul 13, 2010 at 11:28 AM, Shlomi Vaknin
Thank you Bob, your example clarified how actually using such data type would appear in haskell. I naively thought it would be as simple as defining a regular list, but i see it is slightly more strict than that. I appreciate your help! Vadali
Well it _is_ as simple as defining a regular list, which would be (fictionally since (:) and [] are reserved identifiers) :
data [] a = [] | a : [a]
Which is the same as :
data List a = Empty | Cons a (List a)
You can then handle lists with pattern matching :
map f [] = [] map f (x:xs) = f x : map f xs
Or for our List type :
map f Empty = Empty map f (Cons x xs) = Cons (f x) (map f xs)
His definition of a tree :
data Tree a = Leaf | Branch a [Tree a]
follows the same idea and is as easy to handle with pattern matching :
treeMap f Leaf = Leaf treeMap f (Branch x xs) = Branch (f x) (map (treeMap f) xs)
As you see, an user defined type is manipulated with the same mechanism as a "primitive" type, this uniformity is part of the power of Haskell in that it encourages users to create their types and allows seamless integration of external library types with "primitive" types. -- Jedaï

Chaddaï and Richard,
Both your reply's helped me alot! It is so much different then imperative
programming, and i say it as a good thing. I still have lots to learn, but
its just like math, it looks so obvious when you see the solution, but
entirely different when you have to face it yourself, another point of
similarity with math, the more you practice, the better you get :)
Richard, your definition of List_Or t made the coin drop in my head (since i
knew almost nothing about datatypes), and I has managed to write a little
"flatten" function for these nested lists. I am now trying to think how to
make my solution nicer (hehe dont spoil it yet by showing me yours yet! :) )
You all have a great weekend,
Vadali
On Wed, Jul 14, 2010 at 12:09 PM, Chaddaï Fouché
Thank you Bob, your example clarified how actually using such data type would appear in haskell. I naively thought it would be as simple as defining a regular
On Tue, Jul 13, 2010 at 11:28 AM, Shlomi Vaknin
wrote: list, but i see it is slightly more strict than that. I appreciate your help! Vadali
Well it _is_ as simple as defining a regular list, which would be (fictionally since (:) and [] are reserved identifiers) :
data [] a = [] | a : [a]
Which is the same as :
data List a = Empty | Cons a (List a)
You can then handle lists with pattern matching :
map f [] = [] map f (x:xs) = f x : map f xs
Or for our List type :
map f Empty = Empty map f (Cons x xs) = Cons (f x) (map f xs)
His definition of a tree :
data Tree a = Leaf | Branch a [Tree a]
follows the same idea and is as easy to handle with pattern matching :
treeMap f Leaf = Leaf treeMap f (Branch x xs) = Branch (f x) (map (treeMap f) xs)
As you see, an user defined type is manipulated with the same mechanism as a "primitive" type, this uniformity is part of the power of Haskell in that it encourages users to create their types and allows seamless integration of external library types with "primitive" types.
-- Jedaï

2010/7/13 Ivan Lazar Miljenovic
vadali
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
participants (9)
-
Chaddaï Fouché
-
Christopher Done
-
Ivan Lazar Miljenovic
-
Johan Tibell
-
Richard O'Keefe
-
Shlomi Vaknin
-
Thomas Davie
-
vadali
-
Vo Minh Thu