[Haskell-beginners] Questions About Rose Trees Analysis

Lorenzo Isella lorenzo.isella at gmail.com
Sun Dec 20 19:59:54 UTC 2015


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


More information about the Beginners mailing list