Please consider the following definition of a rose tree in Haskell
data Rose a = a :> [Rose a]
deriving (Eq, Show)
root (a :> rs) = a
children (a :> rs) = rs
together with the implementation of the functions to get the root and the children of a rose tree. The bit where I am still struggling is how to implement the functions
size :: Rose a -> Int
leaves :: Rose a -> Int
that count the number of nodes in a rose tree, respectively the number of leaves (nodes without any children). Can anybody help?
size (_ :> ts) = ? + sum ?
leaves (_ :> []) = ?
leaves (_ :> ts) = sum ?
You will need to use recursion to fill in the blanks, and a common higher-order function.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.