[Haskell-cafe] lists of arbitrary depth
tom.davie at gmail.com
Tue Jul 13 05:18:58 EDT 2010
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:
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.
* In this case an arbitrarily branching tree, we could ofc declare different forms of tree here.
More information about the Haskell-Cafe