[Haskell-cafe] Trees (Rose Trees?)

Ryan Bloor ryanbloor at hotmail.com
Mon Jul 21 22:33:42 EDT 2008


hi
 
I was curious as to whether my implementation of a Rose Tree and a sumTree function was correct. The aumTree adds up the elements of a tree.
 
data Tree a = Leaf a | Node [Tree a]
 
sumTree :: Tree Int -> Int
sumTree (Node []) = 0
sumTree (Node xs) = sum (map sumTree xs)
 
The problem with this is I get a pattern matching error. Am I representing trees right... see below.
 
Also, would an empty tree be represented by ... Node [] with this implementation?
How would I represent a tree of the form... Tree (Node 2(Node 6 Empty Empty) Empty) taken from a binary one.
Like this? Node [ [Leaf 2], Node [ Leaf 6,Node[],Node[] ], Node[] ] 
 
Ryan
 
 
_________________________________________________________________
Find the best and worst places on the planet
http://clk.atdmt.com/UKM/go/101719807/direct/01/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20080722/4081a483/attachment.htm


More information about the Haskell-Cafe mailing list