[Haskell-beginners] Questions About Rose Trees Analysis

jean verdier verdier.jean at gmail.com
Sun Dec 20 23:22:39 UTC 2015


I think that your definition of size is not what you say it is.
I guess that you expect (size (1 :> [2 :> [ 3 :> [] ] ])) to be 3.

To count leaves you may start with something like:
countleaves (_ :> []) = 1
You have then to define countleaves for the case it's not a leaf.

I hope this is of some help.

On Sun, 2015-12-20 at 20:59 +0100, Lorenzo Isella wrote:
> Dear All,
> This is where I stand now. Please consider the short snippet
> 
> data Rose a = a :> [Rose a]
>             deriving (Eq, Show)
> 
> root (a :> rs) = a
> 
> children (a :> rs) = rs
> 
> size :: Rose a -> Int
> 
> size (a :> rs) = 1 + (length $ children (a :> rs))
> 
> which defines a rose tree structure and the functions to get the
> root, the children and the size (i.e. number of nodes) of the rose
> tree.
> I would like to find the number of leaves (i.e. terminal nodes) in my
> rose tree.
> Essentially, I need to count the number of "[]" inside my rose tree
> definition.
> For instance, consider
> 
> mytree = (1 :> [2 :> [], 3 :> []])
> 
> which has exactly two leaves.
> Can anyone help me implement a function to get the number of leaves?
> Many thanks
> 
> Lorenzo
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


More information about the Beginners mailing list